`on('notification')` is not triggered on iOS when app is not running - ios

I'm using the phonegap-plugin-push on my ionic3 app and I've been facing this issue with the iOS platform (android is ok). When the app is in background/foreground everything works as expected. When the app is not running however, I get the message on the notification tray but neither tapping the notification or the app icon triggers the on('notification') event, it simply runs and ignore the incoming notification.
I'm using the { ... content-available: 1} in the aps json message as suggested by the docs, but it doesn't seem to have any effect (though, it does have effect on Android), the ionic app itself never gets the message even though the device received it and displayed it on the tray.
Bellow, I show my configurations and a sample message sent to the push services. If anyone have any idea on how to sort this out will be appreciated, I've tested on ios 8.x and ios 11.x, both have presented the same behavior.
const options: PushOptions = {
android: {
senderID: '*************',
icon: 'ic_stat_ic_notification'
},
ios: {
alert: 'true',
badge: 'true',
sound: 'true'
},
windows: {}
};
platform.ready().then(() => {
push.on('registration').subscribe((data: EventResponse) => {
// register data.registrationId
});
push.on('notification').subscribe((data: EventResponse) => {
alert(data.message)
});
});
Sample message:
{
"aps": {
"alert": "This is a Push",
"badge": 1,
"content-available": 1
},
"payload": {
"type": "news"
}
}

This is too long of a comment. It's more of an indirect answer.
1) or the app icon triggers the on('notification') event <-- that's expected. Imagine your app just received 10 notifications. How should it know which notification you intended to open?
2) neither tapping the notification <-- this should trigger something in your code. Do you have didReceiveNotificationResponse implemented? Does that get called? Or does your didFinishLaunching get launched with a remoteNotification key? How about your application(_:didReceiveRemoteNotification:fetchCompletionHandler:) does that get called?

Related

How to show local notification upon receiving silent push?

We are building a react-native IOS application, which uses FCM at the backend and notifee at the front end. Everything works, but there are often times when a FCM-data-push is sent but the notification is not shown on IOS if the app is in killed state. Following is the message object that we are sending to the FCM server through firebase-admin's node package.
const message = {
token: fcmToken,
data: prepared,
android: {
priority,
},
apns: {
payload: {
headers: {
'apns-priority': 10,
'apns-push-type': 'background',
},
aps: {
'content-available': 1,
},
},
},
};
It is required to register a task that runs in the background while the app is killed then the registered task handle notification event listens and pushes notification.
Consult this package for background task - https://www.npmjs.com/package/react-native-background-task

iOS Push notifications using cordova. The notification is never received

I created a sample Cordova app which is using "phonegap-push-plugin".
That app doesn't have any complexity. On "deviceready" I run the plugin initialization code as shown here:
var push = PushNotification.init({android: {}, ios: {
sound: true,
alert: true,
badge: true,
categories: {
invite: {
yes: {
callback: 'accept',
title: 'Accept',
foreground: true,
destructive: false
},
no: {
callback: 'reject',
title: 'Reject',
foreground: true,
destructive: false
},
maybe: {
callback: 'maybe',
title: 'Maybe',
foreground: true,
destructive: false
}
},
delete: {
yes: {
callback: 'doDelete',
title: 'Delete',
foreground: true,
destructive: true
},
no: {
callback: 'cancel',
title: 'Cancel',
foreground: true,
destructive: false
}
}
}
}})
push.on('notification', data => {
console.log(data.message);
console.log(data.title);
console.log(data.count);
console.log(data.sound);
console.log(data.image);
console.log(data.additionalData);
})
push.on('emailGuests', data => {
console.log('I should email my guests');
});
push.on('snooze', data => {
console.log('Remind me later');
});
push.on('registration', data => {
console.log(data.registrationId);
console.log(data.registrationType);
});
push.subscribe('xx', console.log)
And this is the log output to console:
=> Successfully subscribe to topic xx
// The first run (after app install) will ask for permissions. If I click allow the lines below are printed to console.
=> dCAtjhCFBcU:APA91bG90c8VhNl_BzZ-2e9fmq_9fN6jfrRNJ1LPCRIpKnZ-AG-eLY4xtX84oJRZBh2D....KtNNQ35GM8ubPF5zr8HqeB6jffs
=> FCM
In order to push I'm sending the following payload to the Legacy Server https://fcm.googleapis.com/fcm/send.
{
"priority": "high",
"to": "/topics/xx", // I tried this but I also tried to specify the device token received upon "registration" event. I did this using to:<device_token> and also using registration_ids: [<device_token>].
"notification": {
"title": "My Message",
"body": "My Message Body",
"badge": 1,
"content-available": "1", // I tried with and without
"category": "identifier", // I tried with and without
"thread-id": "id", // I tried with and without
"sound": "default",
"icon": "default"
},
"data": {
"title": "A short string describing the purpose of the notification",
"body": "The text of the alert message",
"clubId": 1000
},
"notId": 1,
"custom_key1": "value1",
"custom_key2": "value2"
}
Note: I tried every combination possible in what concerns the app state: App in background; app closed; app in foreground; The event "notification" has never fired and the push notification was never received.
The request sent to the FCM server returns a message id when I use the topic (which is understandable since other devices subscribe the topic). For that reason my android that has subscribed to the same topic receives the message. The iOS in the other hand receives nothing!
{
"message_id": 5059997308576486332
}
If I try to specify the token that I received upon registration, I will get a slightly different message. Most of the time the token received upon registration works and the results will contain a string id. But this is temporary since a few minutes later the token become "NotRegistered".
{
"multicast_id": 88880398234xxxxx7,
"success": 0,
"failure": 1,
"canonical_ids": 0,
"results": [
{
"error": "NotRegistered" // <-- This happens after a few minutes. I have to delete the app and reinstall it in order to get a new token.
}
]
}
This is the build configuration
Notifications are correctly enabled on my iOS device. What am I missing?
Updated:
Accessing Apple's APN directly (yup... no FCM!)
I would like to send my push notifications through FCM but in order to determine the cause of the issues described above, I decided to try APN directly. To do this, I had to remove the from the app's config.xml, so phonegap-push-plugin can obtain a token from APN and not from FCM.
Now, using the new token and a server that uses node-apn module to communicate with APN server, I'm able to send push notifications to my iOS app. The downside of this, is that I lose the ability to push to topics since this is a FCM only feature.
The only thing that I still don't know is how to use the topic to target devices in the APN network, that are subscribed by the push.subscribe() method.
Checkout my issue here.
Any help on this too?
So, turns out there's an issue with the push plugin.
This issue only impacts users on iOS device apps that use the FCM.
If you use the APNs it works.
You can check my answer in here: https://github.com/phonegap/phonegap-plugin-push/issues/2644#issuecomment-445346335
And my initial issue reporting in here:
https://github.com/phonegap/phonegap-plugin-push/issues/2613

Push notification not receive with content_available

I'm working with firebase to add push notification feature to an app, everything is fine with the push notification it self but in some state of the app the push notification is not receive.
Foreground / Background:
{
"data":{
"title": "title",
"message": "message"
},
"content_available":true,
"priority":"high",
"registration_ids":[
""
]
}
The problem comes when the app is killed by the user, I send the same http request but the app didn't receive the remote notification.
I was testing with the console on firebase and when the app is killed the notification it's receive but I couldn't replicate the console request to http request
this is the content of notification when I made it through firebase console
[
"google.c.a.c_l": label optional,
"google.c.a.e": 1,
"google.c.a.ts": IntValue,
"google.c.a.udt": 0,
"gcm.n.e": 1,
"aps": {
alert = {
body = mensaje;
title = title;
};
},
"google.c.a.c_id": IntValue,
"gcm.message_id": 0:1502103735577088%9218dc2f9218dc2f
]
This is how iOS works, basically. When your app is killed by the user, it no longer receives background notifications.
Here's a somewhat-old-but-still-accurate link with some more detailed information: http://samwize.com/2015/08/07/how-to-handle-remote-notification-with-background-mode-enabled/

App icon badge visible by default

I am having a hard time to make this work. What Im using:
1 - The Ionic Framework (version 1.7.14)
2 - Ionic push which is instantiated with: Ionic.Push. So Im not using ngcordova for push
3 -A real device, an Iphone 6s. And I am on OSX.
Since I began sending push notifications from Ionic Push
http://docs.ionic.io/docs/push-overview
something strange happened. I have sent a certain number of pushes both from curl and my own backend application.
The problem was that that badge with number "1" never disappeared from the app icon. I have tried to delete and reinstall the app using Iphone and xcode. But each time the app is installed, the badge with the number 1 is directly shown on the app icon. And even when I go inside the app and then close it, then the badge is still there. Why?
I didnt really understand who should take care of resetting the badge on the app icon. Is it the javascript code in Ionic?
SHouldn't it be managed by default by the OS? Shouldn't the OS clear the badge on the app icon as soon as you open the app that has badges?
I had this code in my js-controller:
var push = new Ionic.Push({
"debug": true,
"pluginConfig": {
"ios": {
"badge": false,
"sound": true
},
"android": {
"iconColor": "#343434"
}
}
});
However, it didnt help. The badge on the app icon is still there.
So as a last thing I did the following:
1 - I removed the code for push in javascript.
2 - I opened "settings" on my iphone and disabled my app to be updated in background.
No luck. The damned badge is still there in the right top corner of my app icon.
Try the following
Change "badge" : false to "badge" : 0
Because badge denotes the total count of notification and not boolean Value for show/hide.
Give it as 0
Hope it helps..
According to Ionic documentation on push, options specified in pluginConfig are passed to Phonegap Push Plugin.
If you look at the plugin documentation you'll see that clearBadge option is what you are looking for, this should be specified both for android and ios, like this:
"pluginConfig": {
"ios": {
"badge": true,
"sound": true,
"clearBadge": true
},
"android": {
"clearBadge": true
}
}
If you are using Phonegap plugin push, you can also do this.
var pushNotification = PushNotification.init(
{ "android": {"senderID": config.senderId, "icon": "notification","clearBadge":"true"},
"ios": {"alert": "true", "badge": "false", "sound": "true", "clearBadge": "true"},
"windows": {} } );
pushNotification.on('notification', function(data) {
pushNotification.setApplicationIconBadgeNumber(function() {
console.log("clear badge success");
}, function() {
console.log("clear badge error");
}, 0); //setting 0 will clear the badge
console.log("notification " + JSON.stringify(data));
});
You can see the doc here

GCM (Google Cloud Messaging) push notification from python app server to iOS app

I'm trying to send push notifications to my iOS app with GCM. I tried to follow the (Google GCM) and (GCMServerDemo) for my app but I only can receive the notification printed on the xcode output: [body: hello, it's me, sound: default, collapse_key: do_not_collapse, badge: 2, from: 629354528047] but nothing pop out on my testing phone. My server running on python and I have it implemented as:
from gcm import *
gcm= GCM("123...")
DEV_TOKEN = "l0NOTncXJXQ:APA91bGPVHxvF-PCL-PPNic6zhfnpv0aAe5KhvoYOOF_HfLZlCAquMGQb196J5_4zUEzWEirSOWP86d-n4-DJws4nPs5ZXR1c9UOQOPPuuCAjXFz2VIZ-5_SRz8G6D_MzKHv1W7yRrmZ"
reg_ids = [DEV_TOKEN]
notification = {"body": "hello, it's me", "sound": "default", "badge": 2}
response=gcm.json_request(registration_ids=reg_ids, data=notification)
print(response)
My AppDelegate on my app client to detect notification: http://swiftstub.com/621889661/
My ViewController: http://swiftstub.com/89730359
I want my client app receive the notification both when the app is active and when the screen is off (but right now I only can receive the message sent on my server in the xcode output screen, as [body: hello, it's me, sound: default, collapse_key: do_not_collapse, badge: 2, from: 629354528047])
I guess there is a problem when I parse the received message. Can anyone help me to fix it? Thank you.
This is what the gcm payload should look like if all you want to do is display a message to the user:
{
"to":"bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
"notification":{
"body":"This text message will be seen by the user",
"badge": 2,
"sound": "default"
}
}
It will only be visible by default when the app is not in the foreground. When it is, you need to read the message in your AppDelegate's didReceiveRemoteNotification method, and show it to the user yourself.
Edit:
Instead of using gcm.plaintext_request(registration_id=reg_id,data = data)
you might want to try this:
notification = {'body': "hello, it's me", "sound": "default", "badge": 2}
DEV_TOKEN = '**********************************'
reg_ids = [DEV_TOKEN]
response = gcm.json_request(registration_ids=reg_ids, notification=notification)
notification should be outside the data key

Resources