I have a real head scratcher here.
My company sells 2 apps created with Expo. Both are the same app, but one is white labeled. This means the color scheme and the provisioning are different, but the code/permissions/API are exactly the same.
On my iphone, I installed both apps (A and B). App A is receiving both foreground and background notifications. App B is receiving only foreground notifications.
I am using RNFirebase to get a FCM token and I send this to the API. Then, I use react-native-notifications to register and create event listeners.
Here are my listeners:
import {Notifications} from 'react-native-notifications';
Notifications.events().registerNotificationReceivedForeground((notification, completion) => {
console.log('foreground notification', notification);
completion({alert: false, sound: false, badge: false});
});
Notifications.events().registerNotificationReceivedBackground((notification, completion) => {
console.log('background notification', notification);
completion({alert: false, sound: false, badge: false});
});
For App A I get:
foreground notification [Object object]
background notification [Object object]
For App B I get:
foreground notification [Object object]
I did some extra checks;
checking if the permissions are all the same
the Apple Identifier has the correct capabilities
the JSON request to firebase is correct
content-available = 1 in de aps body
All seems fine.
The above results tell me that there is nothing wrong with my server code and that the client code should be working as well.
Does anybody have an idea where to start looking? (I already rebooted the phone)
Related
We're using this guide to create a pretty routine push notification system.
We have everything working and push notifications are coming through. On Android, the push notifications make the default alert sound. On iOS however, no sound is made.
How can we configure the push notification to use the default alert sound on iOS (we don't want to create/manage a custom alert sound).
I've already configured the presentationOptions setting in the capacitor.config.json file.
{
"appId": "REDACTED",
"appName": "REDACTED",
"bundledWebRuntime": false,
"npmClient": "npm",
"webDir": "www",
"plugins": {
"PushNotifications": {
"presentationOptions": ["badge", "sound", "alert"]
}
}
}
Push notifications appearance in foreground
On iOS you can configure the way the push notifications are displayed when the app is in foreground by providing the presentationOptions in your capacitor.config.json as an Array of Strings you can combine.
Possible values are:
badge: badge count on the app icon is updated (default value)
sound: the device will ring/vibrate when the push notification is received
alert: the push notification is displayed in a native dialog
An empty Array can be provided if none of the previous options are desired. pushNotificationReceived event will still be fired with the push notification information.
"plugins": {
"PushNotifications": {
"presentationOptions": ["badge", "sound", "alert"]
}
}
push-notifications-appearance-in-foreground
What are you using to send the push notification?
I followed the same capacitor guide and faced the same issue, then I did a test by sending the notification from the Firebase Cloud Messaging console and it worked on iOS (the notification made a sound).
I found later that in the code I was using to send the notification (the firebase nodejs admin SDK), I didn't provide a value for the sound attribute. I assumed that since it's not required and it worked on Android, it should also work on iOS. I was wrong!
import * as admin from 'firebase-admin';
const message: admin.messaging.MessagingPayload = {
data: {
...
},
notification: {
title: 'title',
body: 'body',
sound: 'default' // Add this line
},
};
await admin.messaging().sendToDevice(tokens, message);
The docs says that this attribute is only for the Android platform, which is why I didn't set it at first.
PS: I also added the presentationOptions setting mentioned above in the capacitor.config.json file.
I am making a chat application in Ionic 2. I want notifications to appear even when app is in foreground. I have tried using Phonegap Plugin Push and FCM Plugin both and I'm getting notifications when app is in background and when app is killed.
But these plugins didn't show notifications when app is in foreground. So I used Local Notifications Plugin by Katzer. It works for Android perfectly but in IOS I'm facing multiple issues.
When used with Phonegap Plugin Push, the local notification does appear but its click event does not work. Also, the two plugins seem to have some conflicts so when used together, sometimes normal push notifications does not arrive or their click events does not work.
When used with FCM plugin, no local notification arrived.
I also tried using phonegap-plugin-local-notification but again it worked for Android but in IOS, notification arrives in form of alert and also its click event gets called automatically.
I am stuck on this for a long time. Can someone please provide a solution? All I want is to get notification in the notification center when app is in foreground in IOS and also a click event so I can do redirection on click.
Any help would be appreciated.
I was implementing in my cordova app this plugin.
And when device ready I fired this code:
pushNotification.register(
function (result) {
//Do some stuff
}, function (error) {
//Do some stuff on error
}, {
"badge":"true",
"sound":"true",
"alert":"true",
"ecb": "onNotificationAPN"
});
And also implemented below function:
function onNotificationAPN(event) {
if (event) {
if ( event.alert ) {
alert(Recieved alert: + event.alert);
}
if ( event.sound ) {
var snd = new Media(event.sound);
snd.play();
}
if ( event.badge ) {
pushNotification.setApplicationIconBadgeNumber(function() {
//SetApplicationIconBadgeNumber success.
}, function() {
//SetApplicationIconBadgeNumber error.
},
event.badge);
}
}
}
And I am able to receive notifications also in foreground.
I´m having trouble on receiving push notifications (background) on a specific device running iOS 10. Other phones with iOS 9 are doing fine.
Although if I open the app the notification shows a banner that I implemented. Why does not show anything in background state?
Reading the firebase documentation something made me a little confused
According to a github firebase example on the following link https://github.com/firebase/quickstart-ios/blob/master/messaging/FCMSwift/AppDelegate.swift, there is a comment in the didReceiveRemoteNotification method that says:
// If you are receiving a notification message while your app is in
the background, // this callback will not be fired till the user taps
on the notification launching the application. // TODO: Handle data of
notification
So it my app is in background, does not apple do the whole thing to make that default iOS notification?
Does the content_available value interfere on this? I also send notification and data values.
Here is an example of the JSON I send:
{
"content_available": true,
"priority": "high",
"data": {
"post_id": "...",
"push_id": "..."
},
"notification": {
"title": "...",
"body": "..."
},
"registration_ids": ["xxxx"]
}
The expected behaviour would be:
App dead: the system will show the notification
App background: the system will show the notification and call the didReceiveRemoteNotification method.
App active: the system will NOT show de notification and call the didReceiveRemoteNotification method.
Right?
Your assumptions are correct except for number 2. There are several cases where apple will not call didReceiveRemoteNotification you even if you set the content_avaialable.
1) Device has low battery and is in power saver mode
2) App has "background app refresh" disabled
3) Other undocumented scenarios where apple decides not to wake up your app. Apple reserves the right to not deliver notifications for performance reasons.
Having said that, if the user taps the notification you will always get the payload in didReceiveRemoteNotification.
I am creating a hybrid app in ionic and want to show a notification when the app is in foreground. Based on research, the best way to do that is via cordova local notifications but though it works perfectly in Android and shows a banner with sound.
On ios, it only puts the notification in the notification try and doesnt make any sound. Can anyone help me out with this?
Here is my code
cordova.plugins.notification.local.registerPermission(function (granted) {
console.log('Permission has been granted: ' + granted);
cordova.plugins.notification.local.schedule({
text: data._raw.message,
at: alarmTime,
data: data._raw.additionalData.loan_id
});
});
You have to listen for the event when the notification is received. From your code you only schedule a notification to be sent but you do not handle receiving the notification.
This is how you register the event when the notification triggers:
$rootScope.$on('$cordovaLocalNotification:trigger',
function (event, notification, state) {
// Add some logic here:
console.log("received: ", notification);
});
Check the ngCordova docs for more events and information.
I have implemented in my IOS app the new Google Cloud Messaging framework for handle push notifications. After implementation I'm able to receive push notifications only when App is active and in foreground. If App is closed or in background I didn't get the notification alert in my device. In the iOS notifications settings I see my app enabled to receive them.
I was having a similar problem where the app would receive a notification only if the app was running (foreground/background) and wouldn't receive anything if app was killed manually (terminated)
All I had to do to fix that was to add priority & content_available to the notification body when sending it (as #KayAnn pointed out)
Here's how it should look like :
{
"to" : "APA91bHun4MxP5egoKMwt2KZFBaFUH-1RYqx...",
"content_available" : true,
"priority": "high",
"notification" : {
"body" : "Winter is coming!",
"title" : "John Snow"
}
}
Keep in mind that I also have UIBackgroundModes: remote-notification in Info.plist.
Google documentations (in step 4) quotes:
If the GCMExample application is running in the foreground, you can
see the content of the notification printed to the Xcode debug
console; if the GCMExample app is in the background, you will receive
an APNS notification.
So in order to receive messages when the app is in background you have to register APNS as options as below as described here.
_registrationOptions = # {
kGGLInstanceIDRegisterAPNSOption: deviceToken,
kGGLInstanceIDAPNSServerTypeSandboxOption: #YES
};
EDIT:
There are few other things you need to do:
1) While publishing the app move to production certificate
In the JSON Payload:
2) Use "content_available": "true" OR
3) Set the "priority": "high"
EDIT:2
Using content_available and priority high in the same payload conflicts with Apple's recommendation (Apple docs). I have come across this during my testing. In such a scenario the message may be throttled.
Use either / or of these two parameters instead.
A Tip on working use-cases:
- content_available: use when you are sending data only and do not have other notification specific parameters like alert, badge, sound etc. This is because by default a content_available message is a silent push.
- Priority: high: Use it when you are sending a notification which should reach the users immediately, i.e time critical notifications such as game scores.