Skip to content

Commit

Permalink
fix(messaging): ios onNotificationTap
Browse files Browse the repository at this point in the history
fixes: #42
  • Loading branch information
triniwiz committed Mar 11, 2022
1 parent dc3fd90 commit ef94e63
Show file tree
Hide file tree
Showing 30 changed files with 146 additions and 46 deletions.
3 changes: 2 additions & 1 deletion apps/demo-angular/nativescript.config.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { NativeScriptConfig } from '@nativescript/core';

export default {
id: 'org.nativescript.plugindemoangular',
// id: 'org.nativescript.plugindemoangular',
id: 'io.github.triniwiz.nativescript.firebasedemo',
appResourcesPath: '../../tools/assets/App_Resources',
android: {
v8Flags: '--expose_gc',
Expand Down
6 changes: 4 additions & 2 deletions apps/demo-angular/src/app.component.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { Component } from '@angular/core';

import { AppService } from './app.service';
@Component({
selector: 'demo-app',
template: `<GridLayout>
<page-router-outlet></page-router-outlet>
</GridLayout>`,
})
export class AppComponent {}
export class AppComponent {
constructor(appService: AppService){}
}
2 changes: 2 additions & 0 deletions apps/demo-angular/src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ import { NativeScriptModule } from '@nativescript/angular';
import { AppComponent } from './app.component';
import { AppRoutingModule } from './app-routing.module';
import { HomeComponent } from './home.component';
import { AppService } from './app.service';

@NgModule({
schemas: [NO_ERRORS_SCHEMA],
declarations: [AppComponent, HomeComponent],
bootstrap: [AppComponent],
imports: [NativeScriptModule, AppRoutingModule],
providers: [AppService]
})
export class AppModule {}
21 changes: 21 additions & 0 deletions apps/demo-angular/src/app.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { Injectable } from '@angular/core';
import { firebase } from '@nativescript/firebase-core';
import '@nativescript/firebase-messaging';

@Injectable()
export class AppService {
constructor() {
const messaging = firebase().messaging();
messaging.onToken((token) => {
console.log('Firebase onToken', token);
});
messaging.onMessage((message) => {
console.log('Firebase onMessage', message);
});

messaging.onNotificationTap((message) => {
console.log('Firebase onNotificationTap', message);
});

}
}
30 changes: 28 additions & 2 deletions apps/demo-angular/src/main.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,32 @@
import { runNativeScriptAngularApp, platformNativeScript } from '@nativescript/angular';
import { AppModule } from './app.module';
import { firebase } from '@nativescript/firebase-core';
import { Application } from '@nativescript/core';

Application.on('launch', (args) => {
console.log('launch');
});

firebase()
.initializeApp()
.then((done) => {
console.log('initializeApp');
});

const messaging = firebase().messaging();

messaging
.requestPermission()
.then(() => {
console.log('requestPermission', 'done');
messaging.registerDeviceForRemoteMessages().catch((e) => {
console.error('registerDeviceForRemoteMessages', e);
});
})
.catch((e) => {
console.error('requestPermission', e);
});

runNativeScriptAngularApp({
appModuleBootstrap: () => platformNativeScript().bootstrapModule(AppModule),
});
appModuleBootstrap: () => platformNativeScript().bootstrapModule(AppModule),
});
2 changes: 1 addition & 1 deletion packages/firebase-admob/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@nativescript/firebase-admob",
"version": "1.0.0",
"version": "1.0.1",
"description": "NativeScript Firebase - Admob",
"main": "index",
"typings": "index.d.ts",
Expand Down
2 changes: 1 addition & 1 deletion packages/firebase-analytics/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@nativescript/firebase-analytics",
"version": "1.0.0",
"version": "1.0.1",
"description": "NativeScript Firebase - Analytics",
"main": "index",
"typings": "index.d.ts",
Expand Down
2 changes: 1 addition & 1 deletion packages/firebase-app-check/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@nativescript/firebase-app-check",
"version": "1.0.0",
"version": "1.0.1",
"description": "NativeScript Firebase - App Check",
"main": "index",
"typings": "index.d.ts",
Expand Down
2 changes: 1 addition & 1 deletion packages/firebase-auth/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@nativescript/firebase-auth",
"version": "1.0.0",
"version": "1.0.1",
"description": "NativeScript Firebase - Auth",
"main": "index",
"typings": "index.d.ts",
Expand Down
45 changes: 24 additions & 21 deletions packages/firebase-core/index.ios.ts
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,17 @@ export class FirebaseApp {
}
}

const launchQueue = [];

declare const TNSFirebaseCore;

const launchCallback = () => {
launchQueue.forEach((item) => item());
launchQueue.splice(0);
};

TNSFirebaseCore.setOnAppFinishLaunchingCallback(launchCallback);

export class Firebase {
constructor() {
if (firebaseInstance) {
Expand Down Expand Up @@ -352,36 +363,32 @@ export class Firebase {
}
resolve(fbApp);
} catch (e) {
reject(new FirebaseError(e.message))
reject(new FirebaseError(e.message));
}
}
};

if (!UIApplication.sharedApplication) {
Application.ios.addNotificationObserver(
UIApplicationDidFinishLaunchingNotification,
(notification) => {
initApp();
}
)
launchQueue.push(() => {
initApp();
});
} else {
initApp();
}
})
});
}

initializeAppWithPath(path: string, options: FirebaseOptions = null, config?: FirebaseConfig) {
return new Promise((resolve, reject) => {
const initApp = () => {
try {
if (path.startsWith('res://')) {
path = NSBundle.mainBundle.pathForResourceOfType(path.replace('res://', '').replace('.plist', ''), 'plist')
path = NSBundle.mainBundle.pathForResourceOfType(path.replace('res://', '').replace('.plist', ''), 'plist');
} else if (path.startsWith('~/')) {
path = knownFolders.currentApp().path + '/' + path.replace('~/', '');
}

const nativeOptions = FIROptions.alloc().initWithContentsOfFile(path);


if (options?.apiKey) {
nativeOptions.APIKey = options.apiKey;
}
Expand Down Expand Up @@ -430,7 +437,6 @@ export class Firebase {
nativeOptions.trackingID = options.trackingId;
}


FIRApp.configureWithOptions(nativeOptions);

const app = FIRApp.defaultApp();
Expand All @@ -445,20 +451,17 @@ export class Firebase {
}
resolve(fbApp);
} catch (e) {
reject(new FirebaseError(e.message))
reject(new FirebaseError(e.message));
}
}
};
if (!UIApplication.sharedApplication) {
Application.ios.addNotificationObserver(
UIApplicationDidFinishLaunchingNotification,
(notification) => {
initApp();
}
)
launchQueue.push(() => {
initApp();
});
} else {
initApp();
}
})
});
}
}

Expand Down
2 changes: 1 addition & 1 deletion packages/firebase-core/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@nativescript/firebase-core",
"version": "1.0.0",
"version": "1.0.1",
"description": "NativeScript Firebase - Core",
"main": "index",
"typings": "index.d.ts",
Expand Down
7 changes: 7 additions & 0 deletions packages/firebase-core/platforms/ios/src/TNSFirebaseCore.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#import <Foundation/Foundation.h>
#import "UIApplicationDelegate+LaunchExtension.h"
typedef void(^LaunchCallback)();
@interface TNSFirebaseCore: NSObject
+(LaunchCallback) onAppFinishLaunchingCallback;
+(void) setOnAppFinishLaunchingCallback:(LaunchCallback)callback;
@end
12 changes: 12 additions & 0 deletions packages/firebase-core/platforms/ios/src/TNSFirebaseCore.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#import "TNSFirebaseCore.h"

@implementation TNSFirebaseCore
static LaunchCallback _Nullable _onAppFinishLaunchingCallback;
+ (LaunchCallback)onAppFinishLaunchingCallback {
return _onAppFinishLaunchingCallback;
}

+ (void)setOnAppFinishLaunchingCallback:(nullable LaunchCallback)callback {
_onAppFinishLaunchingCallback = callback;
}
@end
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#import "TNSFirebaseCore.h"
#import "UIApplicationDelegate+LaunchExtension.h"
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#import <UIKit/UIKit.h>

@interface NSObject (TNSUIAppDelegateExt) <UIApplicationDelegate>
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(nullable NSDictionary<UIApplicationLaunchOptionsKey, id> *)launchOptions;
@end
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#import "UIApplicationDelegate+LaunchExtension.h"
#import "TNSFirebaseCore.h"
#import <objc/runtime.h>

@implementation NSObject (TNSUIAppDelegateExt)
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(nullable NSDictionary<UIApplicationLaunchOptionsKey, id> *)launchOptions {
LaunchCallback callback = [TNSFirebaseCore onAppFinishLaunchingCallback];
if(callback != nil){
callback();
}
return YES;
}
@end
5 changes: 5 additions & 0 deletions packages/firebase-core/platforms/ios/src/module.modulemap
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module TNSFirebaseCore {
umbrella header "TNSFirebaseCoreUmbrella.h"
export *
module * { export * }
}
2 changes: 1 addition & 1 deletion packages/firebase-crashlytics/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@nativescript/firebase-crashlytics",
"version": "1.0.0",
"version": "1.0.1",
"description": "NativeScript Firebase - Crashlytics",
"main": "index",
"typings": "index.d.ts",
Expand Down
2 changes: 1 addition & 1 deletion packages/firebase-dynamic-links/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@nativescript/firebase-dynamic-links",
"version": "1.0.0",
"version": "1.0.1",
"description": "NativeScript Firebase - Dynamic Links",
"main": "index",
"typings": "index.d.ts",
Expand Down
2 changes: 1 addition & 1 deletion packages/firebase-firestore/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@nativescript/firebase-firestore",
"version": "1.0.0",
"version": "1.0.1",
"description": "NativeScript Firebase - Firestore",
"main": "index",
"typings": "index.d.ts",
Expand Down
2 changes: 1 addition & 1 deletion packages/firebase-functions/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@nativescript/firebase-functions",
"version": "1.0.0",
"version": "1.0.1",
"description": "NativeScript Firebase - Functions",
"main": "index",
"typings": "index.d.ts",
Expand Down
2 changes: 1 addition & 1 deletion packages/firebase-in-app-messaging/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@nativescript/firebase-in-app-messaging",
"version": "1.0.0",
"version": "1.0.1",
"description": "NativeScript Firebase - In App Messaging",
"main": "index",
"typings": "index.d.ts",
Expand Down
2 changes: 1 addition & 1 deletion packages/firebase-installations/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@nativescript/firebase-installations",
"version": "1.0.0",
"version": "1.0.1",
"description": "NativeScript Firebase - Installations",
"main": "index",
"typings": "index.d.ts",
Expand Down
2 changes: 1 addition & 1 deletion packages/firebase-messaging/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@nativescript/firebase-messaging",
"version": "1.0.0",
"version": "1.0.1",
"description": "NativeScript Firebase - Messaging",
"main": "index",
"typings": "index.d.ts",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ public class TNSFirebaseMessaging: NSObject {

@objc public static var onMessageCallback: ((NSDictionary) -> Void)? = nil

public static var onTokenCallback: ((String) -> Void)? = nil

@objc public static var onTokenCallback: ((String) -> Void)? = nil

@objc public static func APNSTokenToString(_ tokenData: NSData)-> String {
var token = ""
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>

@interface TNSFirebaseMessagingLoader: NSObject
@interface TNSFirebaseCoreLoader: NSObject
@end
2 changes: 1 addition & 1 deletion packages/firebase-performance/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@nativescript/firebase-performance",
"version": "1.0.0",
"version": "1.0.1",
"description": "NativeScript Firebase - Performancee",
"main": "index",
"typings": "index.d.ts",
Expand Down
2 changes: 1 addition & 1 deletion packages/firebase-remote-config/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@nativescript/firebase-remote-config",
"version": "1.0.0",
"version": "1.0.1",
"description": "NativeScript Firebase - Remote Config",
"main": "index",
"typings": "index.d.ts",
Expand Down
2 changes: 1 addition & 1 deletion packages/firebase-storage/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@nativescript/firebase-storage",
"version": "1.0.0",
"version": "1.0.1",
"description": "NativeScript Firebase - Storage",
"main": "index",
"typings": "index.d.ts",
Expand Down
Loading

0 comments on commit ef94e63

Please sign in to comment.