GCM registrationId is not received on phonegap push plugin - ios

Couple days ago I had an issue with getting APNS token instead of GCM token while using phonegap push plugin.
Well, I changed setup, I've put senderID in [ios] block, recompiled the app. Now I dont get ANY regustrationId at all on Iphone. It still works fine on Android. Can anyone tell me what can be a problem?
Here is setup of plugin:
var push = PushNotification.init({
android: {
senderID: "8225....8910"
},
ios: {
senderID: "8225....8910",
alert: "true",
badge: "true",
sound: "false"
},
windows: {}
});
And this event is never being called:
push.on('registration', function(data) {
$.ajax({
url: '/authentication/ajax-register-gcm-token/',
data: {token: data.registrationId},
success: function (json) {
alert('Phone registered' + data.registrationId);
}
});
});

The code you have used to initialize Push Notification Plugin Object is wrong. It should be as below:
var push = PushNotification.init({
android: {
senderID: "XXXXXXXXXXXX",
},
ios: {
alert: "true",
badge: "true",
sound: "true",
}
});
push.on('registration', function(data) {
console.log(data.registrationId);
registerDeviceToken(data.registrationId);
});
push.on('notification', function(data) {
console.log("notification event");
alert(JSON.stringify(data));
});
push.on('error', function(e) {
console.log("push error");
alert(JSON.stringify(e));
});
function registerDeviceToken(deviceToken){
//Register the registrationId or deviceToken to your server as per the webservice type and parameters configuration set
//From your code snippet above
$.ajax({
url: '/authentication/ajax-register-gcm-token/',
data: {token: deviceToken},
success: function (json) {
alert('Phone registered' + deviceToken);
}
});
}
There is no SenderID mentioned here in official link as well. Make sure you have turned on Push Notification service under Capabilities section of your Project and you have put correct development and production APNS p12 files and their respective passwords at the server side code. So if you are running App with Development Profile then there should be development p12 file environment sending Push Notification in order to be received on your iOS device. To set up Development and Production APNS p12 certificate, refer this link: APNS setup

Related

Data only messages not receiving on iOS device using Firebase Messaging and React Native

I am sending data only messages from a Firebase Function to a React Native app.
It works on Android, but it not works on iOS.
As specified here https://rnfirebase.io/messaging/usage#data-only-messages
my code for sending a message is the following:
var payload = {
data: {
//my app data
},
apns: {
payload: {
aps: {
contentAvailable: true,
priority: 'high',
},
},
headers: {
'apns-push-type': 'background',
'apns-priority': '5',
'apns-topic': 'com.xxxx.xxxx', //my app bundle id
},
},
tokens: tokens
};
const response = await admin.messaging().sendMulticast(payload);
The response has success=true, so the code is working.
In my iOS app I have this handler:
const setMessages = useCallback(async (remoteMessage: any) => {
console.log('A new FCM message arrived!', JSON.stringify(remoteMessage));
});
useEffect(() => {
const unsubscribe = messaging().onMessage(setMessages);
return unsubscribe;
}, []);
As I said before, it works on Android. In iOS the callback is never triggered.
I am pretty sure I setup the iOS app correctly:
- I can receive notification-only messages in iOS
- I can generate the fcm token

sending notification using firebase admin sdk not working

I am trying to send notification from Node.js server to an ios application. It seems working if I send notification from Firebase console, but isn't working if try from my node.js server using firebase-admin sdk.
I followed tutorial from https://firebase.google.com/docs/cloud-messaging/admin/send-messages.
One thing I do not understand is the response after sending notification seems working. I get below response.
{
"results": [
{
"messageId": "0:1511109840587284%a63b4c28f9fd7ecd"
}
],
"canonicalRegistrationTokenCount": 0,
"failureCount": 0,
"successCount": 1,
"multicastId": 7436388871122493000
}
Does anyone know what I am doing wrong?
-- Edit
Here is the code that sends the notification. admin is the firebase-admin instance.
router.post('/notify', (req, res) => {
admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
databaseURL: "<database>.firebaseio.com"
});
var registrationTokens = [
'tokenFromIosApp'
];
var payload = {
data : {
body : 'TEST'
}
};
admin.messaging().sendToDevice(registrationTokens, payload)
.then((response) => {
console.log('Sent successfully.\n');
console.log(response);
res.status(statusCodes.Ok);
res.json(response);
})
.catch((error) => {
console.log('Sent failed.\n');
console.log(error);
res.status(statusCodes.InternalServerError);
res.json(error);
});
});
To send a notification, the payload must use the notification key:
var payload = {
notification: {
title: 'My Title',
body : 'TEST'
}
};

Push notifications with Ionic and Firebase on iOS

I've used these instructions to setup Push notifications with Firebase on iOS. I'm pretty sure I've setup all the Apple certificates correctly and I can sent the notifications from FCM (Firebase Cloud Messaging) just fine, and the status is "sent", but they never arrive in my iPhone.
https://ionicframework.com/docs/native/push/
Here's my code. Any advice why this is not working or how to debug it will be highly appreciated!! Many thanks!
import { Push, PushObject, PushOptions } from '#ionic-native/push';
constructor(platform: Platform, private push: Push, public alertCtrl: AlertController) {
platform.ready().then(() => {
StatusBar.styleDefault();
Splashscreen.hide();
this.pushNotifications();
});
}
pushNotifications() {
this.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'); }
}).catch((error) => { console.log("Push Notification needs Cordova: " + JSON.stringify(error));});
const options: PushOptions = {
android: {
senderID: 'My_ID'
},
ios: {
alert: 'true',
badge: true,
sound: 'false'
},
windows: {}
};
const pushObject: PushObject = this.push.init(options);
pushObject.on('notification').subscribe((notification: any) => {
if(notification.additionalData.foreground) {
let youralert = this.alertCtrl.create({
title: 'New Push notification',
message: notification.message
});
youralert.present();
}
});
pushObject.on('registration').subscribe((registration: any) => console.log('Device registered', JSON.stringify(registration)));
pushObject.on('error').subscribe(error => console.error('Error with Push plugin', error));
}
You should add the FCM plugin find in the link:
https://ionicframework.com/docs/native/fcm/
Then after find the below file in plugin :
“AppDelegate+FCM”
Where you can find the below method:
customDidFinishLaunchingWithOptions
Please replace below :
[FIRApp configure];
with this
// [START configure_firebase]
if(![FIRApp defaultApp]){
[FIRApp configure];
}
Have you tried to send your push notifications from online apns tester tools?
(like. http://pushtry.com/). If you still have the same problem check your ios certificates, configurations and device checks again.

Unable get message on IOS from firebase console with phonegap plugin

i have successfully implemented Firebase cloud messaging in my phonegap app with phonegap-plugin-push on IOS, i am receiving token from Firebase on register event, i print it in alert message and it shows me token.
Below is my code
function onDeviceReady(){
var push = PushNotification.init({
"android": {
"senderID": "29271112133",
sound: true,
vibrate: true
},
"ios": {"senderID": "29271112133","alert": "true", "badge": "true", "sound": "true"},
"windows": {}
});
push.on('registration', function(data)
{
globalpushtoken1 = data.registrationId;
alert("Token IS HERE.. "+globalpushtoken1);
checktoken(data.registrationId);
});
push.on('notification', function(data) {
if(data.additionalData.coldstart){
if(data.title=="New Offer !"){
window.location.replace("#offerspage"); }
}
});
push.on('error', function(e) {
alert("Push Error");
console.log("push error");
});
Now the problem is when i try to send some test message from firebase console and select User segment and select my app and send message it shows message status as completed however i dont get any notification on real device, and if i send the message to specific device using token of that device then it shows status as
FAILED Unregistered registration token " I am new to firebase and phonegap, please help me resolve this.
firebase console after sending message manually

Phonegap Plugin Push notification stays in tray when clicked on iOS

I built a phonegap application which receives push notifications. When I run the application on Android, everything works perfectly, but when I run it on iOS, the push notifications stay in the notification tray after clicking them.
I am using Phonegap Push Plugin for the Phonegap application and I'm using PushSharp for sending the notification. This is my code:
var push = PushNotification.init({
"android": { "senderID": "XXXXXXXX", "icon":"pushicon" },
"ios": { "alert": "true", "badge": "true", "sound": "true"},
"windows": {}
});
push.on('registration', function(data) {
var deviceInfo = {
notificationId: data.registrationId,
phonePlatform: device.platform,
phoneVersion: device.version,
phoneModel: device.model
}
info = $.extend(info, deviceInfo);
localStorageService.setDeviceInfo(info);
});
push.on('notification', function (data) {
alert(data.additionalData.title);
});
push.on('error', function(e) {
console.log('Something went wrong: ' + e.message);
});
I've also tried calling push.finish(), but it makes no difference.
Does anybody know how I can make sure a notification on the notification tray dissapears after clicking on it?
Specs
Device: iPod Touch with iOS 7.0.4
Plugin: Phonegap plugin push
On iOS 8 and greater, when you tap the push it will be deleted from notification center, but on iOS 7 and older it doesn't work that way.
If you want to delete the push notification from notification center you'll have to set the badge to 0.
push.setApplicationIconBadgeNumber(function() {
console.log('success');
}, function() {
console.log('error');
}, 0);
If it doesn't work you can try to set it to a greater number first and then to 0
push.setApplicationIconBadgeNumber(function() {
push.setApplicationIconBadgeNumber(function() {
console.log('success');
}, function() {
console.log('error');
}, 0);
}, function() {
console.log('error');
}, 2);
Latest version of the plugin also has a clearAllNotifications method:
push.clearAllNotifications(function() {
console.log('success');
}, function() {
console.log('error');
});
Or you can also pass the clearBadge option to the init method
var push = PushNotification.init({
"android": { "senderID": "XXXXXXXX", "icon":"pushicon" },
"ios": { "alert": "true", "badge": "true", "sound": "true", clearBadge: "true" },
"windows": {}
});

Resources