Get custom data from fcm in xamarin ios - ios

I am working with xamarin ios and firebase cloud messaging.
This is my fcm request body.
{
"registration_ids": ["token1","token2"],
"notification": {
"sound": "default",
"body": "test body",
"title": "test with notification",
"content_available": true,
"priority": "high"
},
"data": {
"notificationType" : "notification"
}
}
I want to retrieve "notificationType" in DidReceiveRemoteNotification method.
This is my method :
[Export("application:didReceiveRemoteNotification:fetchCompletionHandler:")]
[ObjCRuntime.Introduced(ObjCRuntime.PlatformName.TvOS, 10, 0, ObjCRuntime.PlatformArchitecture.All, null)]
[ObjCRuntime.Introduced(ObjCRuntime.PlatformName.iOS, 7, 0, ObjCRuntime.PlatformArchitecture.All, null)]
public override void DidReceiveRemoteNotification(UIApplication application, NSDictionary userInfo, Action<UIBackgroundFetchResult> completionHandler)
{
UIApplication.SharedApplication.ApplicationIconBadgeNumber = 0;
NSDictionary data = userInfo.ObjectForKey(new NSString("data")) as NSDictionary;
string notificationType = Convert.ToString(data[new NSString("notificationType")] as NSString);
}
Let me know what I need to change here?
Thanks.

Related

Phonegap, IOS , Device doesn't receive notification

I am developing a Phonegap application for iOS and would like to enable push notifications. I use GCM and when I send notification google return me success but IOS device doesn't show anything .
There is my code:
"android": {
"senderID": "xxxxxxxx", "icon": "icon", "badge": "true", "iconColor": "white", "sound": "true", "soundname": "nb"
},
"ios": {
"senderID": "xxxxxxxx",
"alert": "true",
"badge": "true",
"sound": "true",
"usesGCM":true,
"sandbox":false,
},
"windows": {}
.
First of all install the plugin of push notification.
phonegap plugin add phonegap-plugin-push
After the write the java script code as below for iOS push
var note = new apn.Notification();
note.badge = 1;
note.sound = "beep.wav"; //path to your sound
note.contentAvailable = 1;
note.alert = { "body" : msg, "action-loc-key" : "Show Me!" , "launch-image" : "mysplash.png"};
//note.payload = {'uuid': data.uuid}; // additional payload
note.device = data.deviceId;
var callback = function(errorNum, notification){
console.log('Error is: %s', errorNum);
//console.log("Note " + JSON.stringify(notification));
}
var options = {
gateway: 'gateway.sandbox.push.apple.com',
errorCallback: callback,
cert: 'test.pem',
key: 'test.pem', // ** NEED TO SET TO YOURS
passphrase: '', // ** NEED TO SET TO YOURS
//port: 2195,
enhanced: true,
cacheLength: 100
}
var apnProvider = new apn.Provider(options);
apnProvider.send(note, myDevice).then( (result) => {
console.log("Result :: ",JSON.stringify(result));
});
It will works.

Displaying Message Body in Push Notification

I'm getting follow message when I receive Push Notification and the same JSON is displayed to user too. I want only the Data to be displayed in the body when user receives the push notification rather than displaying the whole JSON.
under aps["alert"] I get this
{
\"Type\":\"TestType\",
\"NotificationId\":\"40\",
\"ImageUrl\":\"\",
\"UserId\":1,
\"Data\":\"Testing Push Notification\"
}
How to display Data in the message body of Push Notification.
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) {
if(application.applicationState == UIApplicationState.Active) {
if let alertInfo = userInfo["aps"]?["alert"] as? Dictionary<String,String>{
if let title = alertInfo["Data"]{
}
}
}
}
please try to send push notification flowing format and if application staying in background then automatic showing body message :
[aps: {
alert = {
body = "Testing Push Notification";
title = "New Message";
NotificationId= "536"
};
sound = default;
}]
Update correct format For example
{
"aps": {
"alert": "Hello World",
"sound": "default"
},
"Person": {
"Address": "this is a test address",
"Name": "First Name",
"Number": "023232323233"
}
}
iOS Push Notification custom format
The keys in aps are pre-defined by apple. But you can send your custom
keys outside aps as shown below
{
"aps": {
"alert": {
"title": "Hey!🙂 Checkout my custom notification",
"subtitle": "Custom notification subtitle",
"body": "Description of custom notification"
},
"sound": "default",
"category": "CustomPush",
"badge": 1,
"mutable-content": 1
},
"Employee": {
"Name": "John Doe",
"Designation": "Manager"
}
}
Where Employee is custom key where you can set your own data
as required

FCM Notification in iOS doesn't play sound when received

I am using Firebase push notifications in my iOS Application. Although I am able to send the notification by sending below payload, it doesn't play a sound when received.
{
"to": "myToken",
"notification": {
"body": "test",
"title": "test"
},
"priority": "high"
"sound": "default"
}
If I send the test message from console, it works well and plays notification sound. Note:
My Authorization code is correct
I am sending http request to https://fcm.googleapis.com/fcm/send
I have tested it on IPhone 4 , IPhone 6 and IPhone 6S, All recieve
notifications without sound
your JSON "sound" : "default" should be inside the "notification" key not in the root of the JSON. This JSON should work.
{
"to": "myToken",
"notification": {
"body": "test",
"title": "test",
"sound": "default"
},
"priority": "high"
}
When using the FCM admin SDK, you have to specify sounds separately for Android and Apple devices:
let message = {
notification: {
'body': 'This is the message the user sees',
},
data: {
'param1': 'specify some extra data here',
},
// Apple specific settings
apns: {
headers: {
'apns-priority': '10',
},
payload: {
aps: {
sound: 'default',
}
},
},
android: {
priority: 'high',
notification: {
sound: 'default',
}
},
token: 'target FCM token goes here',
};
(Note: I've only tested the Apple settings thus far)
payload = {
notification:{
title: 'SOLO has been changed by an administrator',
body: 'Administrator changed your SOLO schedule',
},
android: {
},
apns: {
headers:{
"apns-collapse-id": "solo_changed_administrator",
"content-available": "1",
"apns-priority": "10",
},
payload:{
aps:{
sound: 'default',
badge: 12213123223
}
}
},
data:{
type: 'type'
}
}
https://firebase.google.com/docs/reference/fcm/rest/v1/projects.messages?authuser=0#ApnsConfig
I had the same problem. When notifications come through FCM to iOS not working sounds or vibrations. Here I followed this link: https://developer.apple.com/library/archive/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/PayloadKeyReference.html#//apple_ref/doc/uid/TP40008194-CH17-SW1
And finally went successful. I created my apple notification as an alert in my payload and it works for me. Here is my JSON below to get some idea about my solution.
"apns": {
"payload": {
"aps" : {
"alert" : {
"body": "Notification body",
"title": "Notification title"
},
"badge" : 2,
"sound" : "default"
}
}
}
Note: Please put this "apns" key in the relevant place of your message request. I was using REST call for requesting message.
Please refer the following link to get a good idea of how to send a notification message with platform-specific delivery options.
Link: https://firebase.google.com/docs/cloud-messaging/concept-options#example-notification-message-with-platform-specific-delivery-options

Firebase Cloud Messing possible bug?

I am trying to send a push notification through FCM, but the response sent to the phone is weird and does not include aps.
I send:
{ "notification" : {
"title" : "this is title",
"body" : "this is body",
"sound" : "defadult"
},
"data": {
"score": "5x1",
"time": "15:10"
},
"time_to_live" : 3000,
"priority" : "high",
"content_available": true,
"to" : "<fcm access token>"
}
and I get the following on the phone:
%# [notification: {
body = "this is body";
e = 1;
sound = default;
sound2 = defadult;
title = "this is title";
}, collapse_key: <bundle-url>, score: 5x1, time: 15:10, from: 844316448558]
This had been working for a while but suddenly broke. Does anyone else experience this?

iOS app receives GCM notifications only by topics

My app implements GCM, but the notifications only receive by topics and not by specific token app.
This it's a example:
{
"registration_ids" : ["dh"],
"content_available" : true,
"priority": "high",
"notification" : {
"body" : "Winter is coming!",
"title" : "John Snow"
}
}
The response:
{
"multicast_id":000000,
"success": 0,
"failure": 1,
"canonical_ids": 0,
"results": [
{
"error": "InternalServerError"
}
]
}
But the device receives the notifications while they was sent by topics.
2015-09-16 22:54:50.931 ExamplePush[1622:379584] Notification received:
{
"collapse_key" = "do_not_collapse";
from = "/topics/global";
message = "{\"id\":102}";
}
Please see this:
Postman 1
Postman 2
You need to add parameter "to" into your request in order to get notification and look at the gcm document as well for more information.
{
"to" : " mh7PX-ODCIE:APA91bEdBMCxTJ45SEcCWRyCfzkkZ74YnJl0q0-J8zgIdcZySgR3wu46RXwBHmSzscI6AuI9inYT145C5j30UPMD7soDKRgTGbrcdgZPUahm_Ik-oIUKoUSLLzGNXBeNqnWKDhpHlBfn",
"content_available" : true,
"priority": "high",
"notification" : {
"body" : "Winter is coming!",
"title" : "John Snow"
}
}
are you checking on postman
If not then try to check in postman
By this url and append the parameters.
https://gcm-http.googleapis.com/gcm/send
set the header also with your api key of gcm.

Resources