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.
Related
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)
I am sending notifications and data messages (non-notification) over APNS from FCM to my iOS App, and everything works fine in the foreground.
I would like to be able to trigger code when the App is in the background.
I already have "Enable Background Modes" "Remote Notifications" set in info.plist
However the following message does not trigger until I bring the App into the foreground
message = new Message()
{
Apns = new ApnsConfig()
{
Headers = new Dictionary<string, string>()
{
{ "content_available", "true" },
},
},
Data = new Dictionary<string, string>()
{
{"temperature", "warm"},
},
Topic = "thetopic",
};
1) can anyone tell me why this does not run immediately. I tried adding
{
"apns-priority", "10"
},
but this makes no difference, and indeed
Local and Remote Notification Programming Guide
states that
It is an error to use this priority for a push notification that
contains only the content-available key.
What am I missing here?
Is there any way at all in iOS that it is possible to cause an non-running App to run up in response to an APNS notification or message, without involving the user?
Is it possible under iOS to have an App permanently running (i.e. autostart) so that it can always respond to a notification or message?
You are sending "data message" - this will not work for apps that are not running. This is very poorly documented but you have to send "notification message" to awake your app. Just add
"notification":{
"title" : "title",
"body" : "body"
},
to your message to get it to work
You should consider creating a UNNotificationServiceExtension for that purpose.
See: https://developer.apple.com/documentation/usernotifications/unnotificationserviceextension
This is in fact a mini-app that is launched each time a notification is received, in which you can put your code.
I am currently working on an app and I have implement firebase Push Notification service into my app. I am recieving notification on my iphone but I am unable to set the custom alert sound that I want.
I added the sound as a .caf
I added the sound to the Copy Bundle Resources
using print (userInfo) I have collected this data that is incomming from Firebase
aps: {
alert = {
body = MSG;
title = Title;
};
sound = default;}, {...}, sound: alarm.caf
I understand where the problem is, I just dont understand how to fix it so that the app plays my custom notification sound.
Problem with Firebase is that you can not send sound parameter like custom parameter with Firebase Console, if you try this as you do, you will get this parameter in app outside of aps Dictionary and the system will not recognize it as a sound to be played although the sound file is into the project.
The only solution for this problem is you have some API/server through which you'll be sent a Firebase notification. When server send you a notification with sound param it will be within aps Dictionary and application will play this sound when the notification arrives.
After much research I have not found any solution.
Under Firebase Messaging Docs, and
Table 2a. iOS — keys for notification messages
it is indicated that Firebase does allow for the Sound key to be included in the payload but as seen in the code printed by didRecieveRemoteNotification,
aps: {
alert = {
body = MSG;
title = Title;
};
sound = default;}, {...}, sound: alarm.caf
Firebase doesn't include the Sound key inside the aps hence not calling the sound key by the app.
The workaround that I used for my app is Easy APNs Provider which is a handy and easy app to use for development purposes, a major issue is that it doesn't have the ability to register and remove notifications automatically.
lastly: for Push Notifications to my published apps, I have opted for a dedicated server which I run off of my website
It's been over 5 years, but if someone still looking for an answer, here is what I did.
Make sure you have configured Firebase Messaging & receiving Notifications.
Add .caf ( sound file ) to the app. It must be visible on
'Target' - > 'Build Phases' & then under 'Copy Bundle Resources'
Delete the app from the device ( This is important )
I use Postman as my server, so message format would be,
{
"notification": {
"title": "Hello",
"sound":"small_message.caf",
"body": "You have a new Job"
},
"to": "your device token"
}
Intro
I'm using Phonegap 4.2 (based on Cordova 5.0) to create a cross-platform app.
For iOS I'm using Xcode 6.0 and I'm using the PushPlugin Cordova plugin to handle Push Notifications.
My Problem
I am able to receive push notifications within the app in the iOS version, but when the app is running in the background then I do not receive any push notifications and they do not appear in the status bar nor in the lock screen. By background I mean when the app is closed.
Details
Referencing
I include the PushPlugin plugin in the config.xml properly:
<feature name="PushPlugin">
<param name="android-package" value="com.plugin.gcm.PushPlugin" />
<param name="ios-package" value="PushPlugin" />
</feature>
I'm referencing the PushPlugin JavaScript object properly in the index.html file:
<script type="text/javascript" src="./js/PushNotification.js"></script>
Attaching the push-notification event
I have properly attached the notification event to the method onNotificationAPN:
pushNotification = window.plugins.pushNotification;
if (device.platform == 'android' || device.platform == 'Android' || device.platform == 'amazon-fireos')
{
// ...
}
else
{
pushNotification.register(tokenHandler, errorHandler,
{
"badge":"true",
"sound":"true",
"alert":"true",
"ecb":"onNotificationAPN"
});
}
tokenHandler and errorHandler are defined and so is onNotificationAPN;
function onNotificationAPN(e)
{
// handle APNS notifications for iOS
if (e.alert)
{
// showing an alert also requires the org.apache.cordova.dialogs plugin
// Note that I have org.apache.cordova.dialogs aswell
navigator.notification.alert(e.alert);
// This code snippet runs fine when the app is open: the app receives the push notification and it's alerted to the user.
}
if (e.sound)
{
// playing a sound also requires the org.apache.cordova.media plugin
// Note that I have org.apache.cordova.media plugin aswell
var snd = new Media(e.sound);
snd.play();
}
if (e.badge)
{
pushNotification.setApplicationIconBadgeNumber(successHandler, e.badge);
// This code snippet works fine when the app is open: the app receives a push notification and when I close the app the badge count is set to 1, whether that's an expected behavior or not I'm not sure but not what matters right now.
}
}
As said, the app does receive and alerts notifications pushed to it when the app is open.
The device does not seem to notice the push, however, when the app is not open. I'd expect the push notification to appear in the lock screen and or in the status bar.
The testing device
I'm testing on an iPad which's OS version is 7.0.3.
Provisioning profile
I'm using a development provisioning profile and the device I'm using for testing has been added to the App's devices in the apple development center.
The push-notification's payload
The payload that's being sent in the Push Notifications looks like this:
Msg: {
"sound":"beeb.wav",
"alert":"Here is a testing push notification",
"badge":"1",
"location":"", // Custom variable
foreground:"1"
}
I've tried changing the foreground variable to 0 and replace foreground with background but it doesn't really change anything.
Notification Center
I have configurated the notification center for the app as it should be:
Badge App Icon is ON.
Sounds is ON.
Show in Notification Center is ON.
Include is set to 5 Recent Items.
Show on Lock Screen is ON.
Help?
I've been looking around a lot but I'm sort of blank, I'd appreciate if Stack-overflow can help. I usually stick to answering questions but now it's my turn to ask :)
The push notification payload need an aps key, and an alert with the message that will be displayed:
For each notification, compose a JSON dictionary object (as defined by
RFC 4627). This dictionary must contain another dictionary identified
by the key aps. The aps dictionary can contain one or more properties
that specify the following user notification types
An alert message to display to the user
A number to badge the app icon with
A sound to play
More info
Payload example:
{
"aps" : {
"alert" : "You got your emails.",
"badge" : 9,
"sound" : "bingbong.aiff"
},
"acme1" : "bar",
"acme2" : 42
}
When the app is in foreground you receive the whole payload and you can handle it even if it doesn't have that format, but when the app is in backgroud or closed, the system needs the aps key and the alert with the message that will be shown on the notification center.
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.