Skip to content
This repository was archived by the owner on Apr 4, 2023. It is now read-only.

Commit 8c2da51

Browse files
#165 Feature/Question: Is it possible to have more control over push notifications prompt
1 parent 8dc2d71 commit 8c2da51

File tree

2 files changed

+69
-27
lines changed

2 files changed

+69
-27
lines changed

docs/MESSAGING.md

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,8 @@ Open `app/App_Resources/iOS/Info.plist` and add this to the bottom:
2626
#### Provisioning hell
2727
Follow [this guide](https://firebase.google.com/docs/cloud-messaging/ios/certs) to the letter. Once you've done it run `tns run ios` and upon starting the app it should prompt you for notification support. That also works on the simulator, but actually receiving notifications is _only_ possible on a real device.
2828

29-
#### Don't receive the "Allow app to receive notifications" prompt?
30-
Make sure your `app.js` has this before `application.start();`:
31-
32-
```js
33-
var firebase = require("nativescript-plugin-firebase");
34-
```
35-
36-
Delete and re-add your app after you've added this.
37-
3829
### Handling a notification
39-
To listen to received notifications while in the foreground or when your app moves from the background to the foreground, add a handler `init`.
30+
To listen to received notifications while in the foreground or when your app moves from the background to the foreground, add a handler to `init`.
4031

4132
Any pending notifications (while your app was not in the foreground) will trigger the `onMessageReceivedCallback` handler.
4233

@@ -53,6 +44,8 @@ Any pending notifications (while your app was not in the foreground) will trigge
5344

5445
You don't _have_ to provide the handler during `init` - you can also do it through a dedicated function.
5546

47+
One scenario where you want to do this is if you don't want the "This app wants to send push notifications" popup during init, but delay it until you call this function.
48+
5649
```js
5750
firebase.addOnMessageReceivedCallback(
5851
function(message) {
@@ -95,3 +88,5 @@ curl -X POST --header "Authorization: key=SERVER_KEY" --Header "Content-Type: ap
9588
* DEVICE_TOKEN: the one you got in `addOnPushTokenReceivedCallback` or `init`'s `onPushTokenReceivedCallback`
9689

9790
<img src="images/push-server-key.png" width="459px" height="220px" alt="Push server key"/>
91+
92+
## iOS push notification popup

firebase.ios.js

Lines changed: 64 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
var firebase = require("./firebase-common");
22
var application = require("application");
3+
var applicationSettings = require("application-settings");
34
var utils = require("utils/utils");
45
var types = require("utils/types");
56
var frame = require("ui/frame");
@@ -56,10 +57,10 @@ firebase.addAppDelegateMethods = function(appDelegate) {
5657
};
5758
}
5859

59-
// making this conditional to avoid http://stackoverflow.com/questions/37428539/firebase-causes-issue-missing-push-notification-entitlement-after-delivery-to ?
6060
if (typeof(FIRMessaging) !== "undefined") {
6161
appDelegate.prototype.applicationDidRegisterForRemoteNotificationsWithDeviceToken = function (application, devToken) {
6262
// TODO guard with _messagingConnected ?
63+
applicationSettings.setBoolean("registered", true);
6364
FIRInstanceID.instanceID().setAPNSTokenType(devToken, FIRInstanceIDAPNSTokenTypeUnknown);
6465
FIRMessaging.messaging().connectWithCompletion(function(error) {
6566
if (!error) {
@@ -95,9 +96,12 @@ firebase.addOnMessageReceivedCallback = function (callback) {
9596
reject("Enable FIRMessaging in Podfile first");
9697
return;
9798
}
98-
9999
firebase._receivedNotificationCallback = callback;
100+
101+
var app = utils.ios.getter(UIApplication, UIApplication.sharedApplication);
102+
firebase._registerForRemoteNotifications(app);
100103
firebase._processPendingNotifications();
104+
101105
resolve();
102106
} catch (ex) {
103107
console.log("Error in firebase.addOnMessageReceivedCallback: " + ex);
@@ -113,8 +117,17 @@ firebase.addOnPushTokenReceivedCallback = function (callback) {
113117
reject("Enable FIRMessaging in Podfile first");
114118
return;
115119
}
116-
117120
firebase._receivedPushTokenCallback = callback;
121+
122+
// may already be present
123+
if (firebase._pushToken) {
124+
callback(firebase._pushToken);
125+
}
126+
127+
var app = utils.ios.getter(UIApplication, UIApplication.sharedApplication);
128+
firebase._registerForRemoteNotifications(app);
129+
firebase._processPendingNotifications();
130+
118131
resolve();
119132
} catch (ex) {
120133
console.log("Error in firebase.addOnPushTokenReceivedCallback: " + ex);
@@ -123,6 +136,22 @@ firebase.addOnPushTokenReceivedCallback = function (callback) {
123136
});
124137
};
125138

139+
firebase.unregisterForPushNotifications = function (callback) {
140+
return new Promise(function (resolve, reject) {
141+
try {
142+
if (typeof(FIRMessaging) === "undefined") {
143+
reject("Enable FIRMessaging in Podfile first");
144+
return;
145+
}
146+
utils.ios.getter(UIApplication, UIApplication.sharedApplication).unregisterForRemoteNotifications();
147+
resolve();
148+
} catch (ex) {
149+
console.log("Error in firebase.unregisterForPushNotifications: " + ex);
150+
reject(ex);
151+
}
152+
});
153+
};
154+
126155
firebase._processPendingNotifications = function() {
127156
if (firebase._receivedNotificationCallback !== null) {
128157
for (var p in firebase._pendingNotifications) {
@@ -145,7 +174,7 @@ firebase._onTokenRefreshNotification = function (notification) {
145174
return;
146175
}
147176

148-
console.log("Firebase FCM token received: " + token);
177+
firebase._pushToken = token;
149178

150179
if (firebase._receivedPushTokenCallback) {
151180
firebase._receivedPushTokenCallback(token);
@@ -161,6 +190,20 @@ firebase._onTokenRefreshNotification = function (notification) {
161190
});
162191
};
163192

193+
firebase._registerForRemoteNotificationsRanThisSession = false;
194+
195+
firebase._registerForRemoteNotifications = function (app) {
196+
if (firebase._registerForRemoteNotificationsRanThisSession) {
197+
// ignore
198+
return;
199+
}
200+
firebase._registerForRemoteNotificationsRanThisSession = true;
201+
var notificationTypes = UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationActivationModeBackground;
202+
var notificationSettings = UIUserNotificationSettings.settingsForTypesCategories(notificationTypes, null);
203+
app.registerForRemoteNotifications(); // prompts the user to accept notifications
204+
app.registerUserNotificationSettings(notificationSettings);
205+
};
206+
164207
// rather than hijacking the appDelegate for these we'll be a good citizen and listen to the notifications
165208
(function () {
166209

@@ -169,16 +212,18 @@ firebase._onTokenRefreshNotification = function (notification) {
169212
firebase._addObserver(kFIRInstanceIDTokenRefreshNotification, firebase._onTokenRefreshNotification);
170213

171214
firebase._addObserver(UIApplicationDidFinishLaunchingNotification, function (appNotification) {
172-
var notificationTypes = UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationActivationModeBackground;
173-
var notificationSettings = UIUserNotificationSettings.settingsForTypesCategories(notificationTypes, null);
174-
var application = appNotification.object;
175-
application.registerForRemoteNotifications();
176-
application.registerUserNotificationSettings(notificationSettings);
215+
// guarded this with a preference so the popup "this app wants to send notifications"
216+
// is not shown until the dev intentially wired a listener (see other usages of _registerForRemoteNotifications())
217+
if (applicationSettings.getBoolean("registered", false)) {
218+
firebase._registerForRemoteNotifications(appNotification.object);
219+
}
177220
});
178221

179-
firebase._addObserver(UIApplicationDidFinishLaunchingNotification, function (appNotification) {
180-
firebase._processPendingNotifications();
181-
});
222+
// this one seems redundant with UIApplicationDidBecomeActiveNotification
223+
// firebase._addObserver(UIApplicationDidFinishLaunchingNotification, function (appNotification) {
224+
// console.log("-- calling _processPendingNotifications from UIApplicationDidFinishLaunchingNotification");
225+
// firebase._processPendingNotifications();
226+
// });
182227

183228
firebase._addObserver(UIApplicationDidBecomeActiveNotification, function (appNotification) {
184229
firebase._processPendingNotifications();
@@ -344,11 +389,13 @@ firebase.init = function (arg) {
344389

345390
// Firebase notifications (FCM)
346391
if (typeof(FIRMessaging) !== "undefined") {
347-
if (arg.onMessageReceivedCallback !== undefined) {
348-
firebase.addOnMessageReceivedCallback(arg.onMessageReceivedCallback);
349-
}
350-
if (arg.onPushTokenReceivedCallback !== undefined) {
351-
firebase.addOnPushTokenReceivedCallback(arg.onPushTokenReceivedCallback);
392+
if (arg.onMessageReceivedCallback !== undefined || arg.onPushTokenReceivedCallback !== undefined) {
393+
if (arg.onMessageReceivedCallback !== undefined) {
394+
firebase.addOnMessageReceivedCallback(arg.onMessageReceivedCallback);
395+
}
396+
if (arg.onPushTokenReceivedCallback !== undefined) {
397+
firebase.addOnPushTokenReceivedCallback(arg.onPushTokenReceivedCallback);
398+
}
352399
}
353400
}
354401

0 commit comments

Comments
 (0)