Phonegap push notification plugin with iOs - ios

I'm using phonegap-plugin-push to send push notifications to Android devices.
I'm trying to figure out how to send notification to iOs.
I've registered at https://developers.google.com/mobile/add?platform=ios&cntapi=gcm&cnturl=https:%2F%2Fdevelopers.google.com%2Fcloud-messaging%2Fios%2Fclient&cntlbl=Continue%20Adding%20GCM%20Support&%3Fconfigured%3Dtrue
I'get token at client app same way as in Android:
var push = PushNotification.init({
android: {
senderID: "5993...475"
},
ios: {
alert: "true",
badge: true,
sound: 'false'
},
windows: {}
});
push.on('registration', function(data) {
$.ajax({
url: '/save-token/',
data: {token: data.registrationId},
success: function (json) {
}
});
});
After I get client's token, I try to send'em push notification:
$to = ModelGcmTokens::getInstance()->getToken($userId);
$API_KEY = 'AIzaS...HcEYC346zQ';
$message = array(
'data' => array(
'title' => ($title) ? $title : 'Test!',
'message' => $message
),
'to' => $to
);
$message = json_encode($message);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"https://gcm-http.googleapis.com/gcm/send");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $message);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
"Authorization:key={$API_KEY}",
"Content-Type:application/json",
));
// receive server response ...
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$server_output = curl_exec ($ch);
curl_close ($ch);
// further processing ....
if ($server_output == "OK") {
return true;
} else {
return false;
}
Google cloud messaging responds with InvalidRegistration error:
{"multicast_id":4701637331500989392,"success":0,"failure":1,"canonical_ids":0,"results":[{"error":"InvalidRegistration"}]}
As I googled this error - it's said that I got client's token wrong.
But I get it with excat same script as on Android. And it works on Android.
The only difference, that I see - is format of the token:
Can anyone tell me how do I get push plugin working on iOs? What's wrong with iOs token and how do I supposed to send messages to GCM, to be received by iOs?
Thanks a lot in advance for any advice. I'm really stuck.

The way you are calling the init method requests the registration ID from APNS not GCM. If you read the documentation on GCM on iOS at the phonegap-plugin-push repository you will see that init needs to be called like this:
var push = PushNotification.init({
android: {
senderID: "5993...475"
},
ios: {
senderID: "5993...475",
gcmSandbox: true,
alert: "true",
badge: true,
sound: 'false'
},
windows: {}
});
If the iOS options has a senderID it will register with GCM.
Also, I notice in the response to one of the comments you don't know the difference between production and development environments on APNS. Well, you really need to read up on this. On iOS devices the APNS environment is different depending if you production and development environment. IF you setup your app as development and you send a push through production it will never arrive at your device.
Now you may be asking why I'm talking about APNS. That's because when you use GCM on iOS you would send your push message to GCM which then will forward your message to the APNS servers and finally to your device.

Related

Push Notification issue in iOS 13

I am using Remote Push Notification feature in my iOS application. Currently i am facing a strange issue. I am receiving push notifications into devices which are have iOS < 13 but those devices which are have iOS 13 not getting push notifications.
I searched a lot over stackoverflow and other platform and found that apple suggested that in iOS 13 we need to add "apns-push-type" into header as well as in payload body. I have done it over server side where i write push notifications related code.
I developed my application using XCode 11 and using FCM (Firebase Cloud Messaging) service to send push notifications.
public function pushSurveyorNotificationios(){
$url = "https://fcm.googleapis.com/fcm/send";
$token = 'XXXX-XXXXX-XXXXX';
$serverKey = 'XXXX-YYYYY-ZZZZZ';
$title = "Push Notification Testing";
$body = "Push Notification Testing detail";
$notification = array('title' =>$title , 'body' => $body, 'sound' => 'default', 'badge' => '1');
$arrayToSend = array('to' => $token, 'notification' => $notification, 'apns' => array('headers' => array('apns-push-type' => 'alert')));
$json = json_encode($arrayToSend);
//die($json);
$headers = array();
$headers[] = 'apns-push-type: alert';
$headers[] = 'Content-Type: application/json';
$headers[] = 'Authorization: key='. $serverKey;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST,
"POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
curl_setopt($ch, CURLOPT_HTTPHEADER,$headers);
//Send the request
$response = curl_exec($ch);
//Close request
if ($response === FALSE) {
die('FCM Send Error: ' . curl_error($ch));
}
curl_close($ch);
return $response;
}
Above function i am using to send push notifications to devices.
I got this link over apple for iOS 13 push notifications issue.
https://developer.apple.com/forums/thread/118420

Push Notification Not Received In IOS Firebase Cloud Functions

Push notifications are working as expected in the android application however nothing is received in IOS. Test notifications are also being received in IOS successfully.
const payload = {
data: {
'key': 'value'
}
};
admin.messaging().sendToDevice(notificationToken, payload)
For anyone stuck on this IOS requires notification to be included so your payload should have the following structure:
const payload = {
notification: {
'title': 'value',
'body': 'value'
},
data: {
'key': 'value'
}
};
admin.messaging().sendToDevice(notificationToken, payload)

How to send push notification to all the users via firebase cloud functions?

Previously I send the push notification using FCM token of the devices for particular user using admin.messaging().sendToDevice(tokens, payload).
Now I'm trying to send the push notification to all the users. But it's not working. Here is my implementation. Is this correct? If wrong how to achieve my task?
const payload = {
notification: {
title: 'Hey! 💛',
body: `Check Choice of the day.`,
sound: 'default',
icon: '',
type: 'Editorial',
badge : '1'
}
};
return admin.messaging().sendToTopic("News",payload)
.then(function(response){
console.log('Notification sent successfully:',response);
})
.catch(function(error){
console.log('Notification sent failed:',error);
});

Nativescript | Firebase notification not working

I'm having a problem using firebase in my Nativescript application, when I'm using android its working great but not working with IOS. the problem is on message sending.
I'm using the push-plugin in the client side
This is the register part in the IOS client side using pushPlugin
const iosSettings:any = {
badge: true,
sound: true,
alert: true,
interactiveSettings: {
actions: [{
identifier: 'READ_IDENTIFIER',
title: 'Read',
activationMode: "foreground",
destructive: false,
authenticationRequired: true
}, {
identifier: 'CANCEL_IDENTIFIER',
title: 'Cancel',
activationMode: "foreground",
destructive: true,
authenticationRequired: true
}],
categories: [{
identifier: 'READ_CATEGORY',
actionsForDefaultContext: ['READ_IDENTIFIER', 'CANCEL_IDENTIFIER'],
actionsForMinimalContext: ['READ_IDENTIFIER', 'CANCEL_IDENTIFIER']
}]
},
notificationCallbackIOS: (message: any) => {
alert(JSON.stringify(message));
}
};
pushPlugin.register(iosSettings, (token: string) => {
// update the token in the server
alert("Device registered. Access token: " + token);
});
}
}, (errorMessage: any) => {
alert("Device NOT registered! " + JSON.stringify(errorMessage));
});
This is how i receive my token to the push notification,
after a got the token when i'm using the pusher application everything works great, i'm getting the notification in the IOS device
but the problem is when im trying to send the notification from the server!.
I get this error :
Invalid registration token provided. Make sure it matches the
registration token the client app receives from registering with FCM.
Node code in my server
var payload = {
data: {
targetId:userToken,
body: "some text"
}
};
var options = {
priority: "high",
contentAvailable: true,
timeToLive: 60 * 60 * 24
};
Admin.messaging().sendToDevice(userToken, <any>payload,options)
.then((response) => {
console.log('notification arrived successfully', response.results[0]);
})
.catch((error) => {
console.log('notification failed', error);
});
registration token for ios is not the same for android. I encountered the same wall you've encountered. You need to use https://github.com/node-apn/node-apn for IOS push notification. and firebase for android notification. You can do the logic on your backend by saving the token. with a field called type which is ios or android if ios you use node-apn and if android use sendToDevice provided by firebase.
Thats what i'm currently using with my current nativescript projects' I'm working on that involves push notification. Hope that helps you, mate.

ionic 3 push notification not received on ios

I am trying to do an android push notification by following this doc i understood that i need
device token
api key
sender id
i am getting device token and i have created a project in FCM and i got sender id and api key .
but i am not able to receive the push notification form the backend her is my code that i have used in app.component.ts file
push.hasPermission()
.then((res: any) => {
if (res.isEnabled) {
console.log('We have permission to send push notifications');
} else {
console.log('We do not have permission to send push notifications');
}
});
// to initialize push notifications
const options: PushOptions = {
android: {
senderID: '129188921379'
},
ios: {
alert: 'true',
badge: true,
sound: 'false'
},
windows: {}
};
const pushObject: PushObject = push.init(options);
pushObject.on('notification').subscribe((notification: any) => console.log('Received a notification', notification));
pushObject.on('registration').subscribe((registration: any) => {
console.log('Device registered', registration);
self.deviceToken = registration.registrationId;
});
pushObject.on('error').subscribe(error => console.error('Error with Push plugin', error));
i am not getting any error how do i check where is the mistake from front end or back end
i am not getting any registredId for iphone what is the issue but for android i am getting
You are calling .hasPermission before push.init

Resources