Push notifications with Ionic and Firebase on iOS - 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.

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

Cordova plugin Firebase did not receives push notifications if app in foreground for iOS application

I am using cordova-plugin-firebase plugin for push notifications,
It works for Android, But in the iOS application does not receive the push notification when the app in the background state.
in iOS onNotificationOpen not working.
try {
window.FirebasePlugin.onNotificationOpen(function (data) {
console.log(JSON.stringify(data));
console.log(data, 'data');
if (data.tap === true)
{
console.log('tapped');
self.ngZone.run(() => self.router.navigate(['/worklist'])).then();
}
else
{
console.log('not tapped');
cordova.plugins.notification.local.schedule({
title: data.title,
text: data.body,
foreground: true,
icon: '',
smallIcon: ''
});
}
});
} catch (e) {
console.log(e);
}
Probably you must set the variable IOS_HIDE_FOREGROUND_NOTIFICACION to false during plugin installation.

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'
}
};

Ionic 2 - iOS NotRegistered

I have done an App in Ionic 2 with the phonegap-plugin-push for notifications.
It works really well with Android but when I try to run it on the iOS it have a few problems.
I can get the registration token but when I do a push message I get the error: NotRegistered as response.
What can it be? The wrong certeficates or some bug in code?
I'm using Ionic 2 on a windows, using phonegap to compile the app to iOS so I have to generate the certificates with openSSL.
There is a few of the code for the push notifications:
config.xml
<plugin name="phonegap-plugin-push" spec="1.8.2">
<variable name="SENDER_ID" value="883847118563"/>
</plugin>
app.component.ts
import {Push, PushObject, PushOptions} from "#ionic-native/push";
(...)
platform.ready().then(() => {
(...)
this.initPushNotification();
});
initPushNotification() {
if (!this.platform.is('cordova')) {
(...)
}
const options: PushOptions = {
android: {
senderID: "8838XXXXXXXX"
},
ios: {
senderID: "8838XXXXXXXX" ,
gcmSandbox: "true",
alert: "true",
badge: false,
sound: 'true'
},
windows: {}
};
const pushObject: PushObject = this.push.init(options);
pushObject.on('registration').subscribe((data: any) => {
(...)
});
pushObject.on('notification').subscribe((data: any) => {
(...)
});
pushObject.on('error').subscribe(error => console.error('Error with Push plugin', error));
}
app.module.ts
import { CloudSettings, CloudModule } from '#ionic/cloud-angular';
const cloudSettings: CloudSettings = {
'core': {
'app_id': 'bdeXXXX'
},
'push': {
'sender_id': '8838XXXXXXXX',
'pluginConfig': {
'ios': {
'badge': true,
'sound': true
},
'android': {
'iconColor': '#ff0000'
}
}
}
};
Thank you in advance for the help.

GCM registrationId is not received on phonegap push plugin

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

Resources