Swift - Push Notification JSON format - ios

Please let me ask about Push Notification issue here.
Our server sent out the data in JSON format like this
{
"notification":
{
"body”:”Test Push Notification (42)”,
"node":"1233837”,
"content-available":"1"
},
"priority":"high"
}
The process of sending out push notification to our app is
We sent out notification from our sever to GCM
From GCM, sending out to APNS.
But when we receive push notification in our app, the format is totally changed (as shown in attached image).
If you face with this kinda issue before, could you share with how to solve it, please?
Or is there any way to change in GCM to get normal JSON format?

Try next format to send notification to GCM:
{ "notification": { "body”:”Test Push Notification (42)”, "node":"1233837” }, "priority":10, "content-available":true }
From APNS you app get some like this
{
aps:{
alert:{
*bla bla bla*
},
*bla bla bla*
}}
Just parse it in didReceiveRemoteNotification: method.

Related

Not getting notifications in receiver side

I am using two firebase database projects. I have to send notifications from one database(Project-A) to another database(Project-B). I am sending notifications through API calls in cloud functions( in Project-A) and have to receive in IOS device ( from Project-B). I am sending the device token which is generated by using Project-A's SenderId through API call and saving that token in Project-A's database. In Project-A, using cloud function I am sending a notification to the IOS device in Project-B. In Project-A's firebase console it showing the notification is successfully sent, but in the receiver side, I am not getting any notification.
Payload:
let payload = {
notification: {
title: "Notification Alert",
body: "Notification Body",
sound: "default",
},
'data':{// these is the data it calls in the messagereceived method
'SenderID':senderauthid,
'ReceiverId':receiverauthid,
'SenderPhoneNumber':String(senderphonenumber),
'SenderName':Sendertotalname,
'flag':"3",
}
};//payload
return admin.messaging().sendToDevice(RegisterationTokens, payload).then((response)=> {
console.info("Successfully sent notification")
return res.status(200).json({Token : "Successfully sent notification"});
}).catch(function(error) {
console.warn("Error sending notification " , error)
});//end of notification
On successful notification message sent, I am receiving an error message stating that
errorInfo:
{ code: 'messaging/invalid-apns-credentials',
message: 'A message targeted to an iOS device could not be sent because the required APNs SSL certificate was not uploaded or has expired. Check the validity of your development and production certificates.' },
codePrefix: 'messaging' }
I have already added "APNs Auth Keys" in "APNs Authentication Key" section in firebase console. Do I have to make any changes?

Firebase Cloud Messaging to Android works but iOS fails. How to structure payload for iOS?

I can send test notifications from Firebase console to my app both on iOS and Android. Therefore, my app is set up properly to receive push notifications on both platforms. However, when I use a cloud function to send notifications. Only, notifications on the Android device are received. No notifications show up on the iOS device. I suspect this may be related to the way that I create the payload in my cloud function. Maybe I am missing something for iOS. If you could give me some tips, that would be great.
I checked if the deviceToken for the iOS device is correct and it was correct.
I sent a test message using firebase console to the same deviceToken for the iOS device and the notification was delivered.
Therefore, I concluded my problem may arise from the cloud function I wrote. Thus, I share below the cloud function:
exports.notifToApp = functions.database.
ref(`/memInfo/{memId}/notifChoice/`).onWrite((snap, context) => {
//send only if exists and new notification OR if doesn't exist
if ((snap.before.exists() && (snap.after.val() !== snap.before.val())) || !snap.before.exists()) {
//get notification body
const notificationTitle = snap.after.val().memName;
const notificationText = snap.after.val().notifText;
//get and loop over notification subscribers
return admin.database().ref(`/notifics/${context.params.memId}/notifSubs/`).once("value", subs => {
if (subs.exists()) {
return subs.forEach(sub => {
//payload for notification
const payload = {
"notification":{
"title": notificationTitle,
"body": notificationText,
"sound": "default",
"click-action": "FCM_PLUGIN_ACTIVITY",
"priority": "high"
}
}
//deliver notification
return admin.messaging().sendToDevice(sub.val().deviceToken, payload).catch(e => {console.log(e);});
});
} else { //end: if returned any value
return 0;
}
});// end: get and loop over notification subscribers
} else { //end: send only if exists and new notification OR if doesn't exist
return 0;
}
});
I do not get any error messages. Function completes successfully with status "OK."
I test using two devices: one android and one iOS. Both device tokens are saved correctly in the database for the cloud function to retrieve and use for sending messages.
I see the notification on the Android device running my app. I wish the notification to show up on the iOS device running the same app.
Test message notification sent from the firebase console shows up on both devices correctly.
I realized that sendToDevice() used the legacy version of payload. I used send() in my function to use the newer version. (see answer: stackoverflow)
admin.messaging().send(payload).catch(e => console.log(e));
I changed the payload to include platform specific fields according to the latest guidelines (see firebase docs)
const payload = {
"token": sub.val().deviceToken,
"notification":{"title": notificationTitle,"body": notificationText},
"android": {"notification": {"sound": "default"}},
"apns": {"payload": {"aps": {"sound": "default"}}}
};
Now it works on both platforms.

Are Actionable notifications in iOS only local notifications or they can be sent from the server as well?

Are Actionable notifications in iOS only local notifications, or can they be sent from the server?
I'm interested in adding an actionable notification with two buttons in the app. I read the official documentation and I'm not clear if this type of notification can only be only local notification or if they can be sent from the server as well.
The linked documentation even gives an example of how to do it. So yes, you can create actionable notifications from remote (push) notifications. Just send along the category identifier in the aps dictionary of the notification payload as described in the docs:
{
“aps” : {
“category” : “MEETING_INVITATION”
“alert” : {
“title” : “Weekly Staff Meeting”
“body” : “Every Tuesday at 2pm”
},
},
“MEETING_ID” : “123456789”,
“USER_ID” : “ABCD1234”
}
And then it will show the actions associated to that category.

Amazon SNS invalid message structure for APNS

I've implemented Amazon SNS for push notifications.
I'm sending json in the following structure:
{
"aps":{
"alert":{
"loc-args":["ARGS"],
"loc-key":"KEY",
}
"sound":"default"
}
}
But on iOS client I'm receiving it as escaped string and everything from my message is inside "alert" key
[AnyHashable("aps"): {
alert = "{\"aps\":{\"alert\":{\"loc-args\":[\"ARGS\"],\"loc-key\":\"KEY\"},\"sound\":\"default\"}";
}]
I've also tried different formats from here http://docs.aws.amazon.com/sns/latest/dg/mobile-push-send-custommessage.html but every-time I'm receiving the same escaped string.
Does anyone had the same problem?
So the problem was in publishRequest.setMessageStructure("json"); flag.
I've missed it.
Also notification should be sent in the following format:
{"APNS_SANDBOX":"{\"aps\":{\"alert\":{\"loc-args\":[\"ARGS\"],\"loc-key\":\"KEY\"},\"sound\":\"default\"}}

How to add the custom information with the push notification using the 'Notificato' plugin?

I am sending push notifications from php job application to iphone. I am sending push notifications regarding new jobs. Is this possible that when user click on the view of push notification pop up , then user redirect to the particular job in the device.
I mean I wanted to know can I send any custom data with push notification like access_token, something else....so that Iphone end Can retrieve and show the particular job ?
Regardless of the language and library you use, the push notification payload is a JSON payload:
{
"aps": {
"badge": 10,
"alert": "Hello world!",
"sound": "cat.caf"
}
}
We want add custom information with the push notification. Like below
{
"aps": {
"badge": 10,
"alert": "Hello world!",
"sound": "cat.caf"
},
"access_token": 1
}
Thanks.
Finally, find the solution, How to add custom information with the push notification... We can add the custom information using the setPayload(). The apnspush() class file like this
<?php namespace Notificato\apnspush;
// This imports the Composer autoloader
require ROOT . "/vendor/autoload.php";
use Wrep\Notificato\Notificato;
class apnspush
{
/**
* This example sends one pushnotification with an alert to Apples production push servers
*/
public function sendOnePushNotification($deviceToken,$message,$accessToken)
{
$passphrase = 'asded333';
// First we get a Notificato instance and tell it what certificate to use as default certificate
$notificato = new Notificato("CBDEV.pem",$passphrase);
// Now we get a fresh messagebuilder from Notificato
// This message will be send to device with pushtoken 'fffff...'
// it will automaticly be associated with the default certificate
// and we will set the red badge on the App icon to 1
/* Make the JSON with access_token */
$pays = ["access_token"=>$accessToken];
$message = $notificato->messageBuilder()
->setDeviceToken($deviceToken)
->setAlert($message)
->setBadge($badge)
->setPayload($pays)
->setSound()
->build();
// The message is ready, let's send it!
// Be aware that this method is blocking and on failure Notificato will retry if necessary
$messageEnvelope = $notificato->send($message);
// The returned envelope contains usefull information about how many retries where needed and if sending succeeded
echo $messageEnvelope->getFinalStatusDescription();
}
}
?>

Resources