Nativescript | Firebase notification not working - ios

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.

Related

How can I add 3 custom sounds in react native iOS?

I want to add 3 custom sounds in react native iOS.
Have any of you ever solved it?
Currently, when I test the FCM Notification by adding and grouping a voice file (.wav) to the iOS project folder, one of the added sounds is coming out.
For example, suppose you have sound files sound01, sound02, sound03, when the backend sends an FCM notification, I want the specified sound to sound at that time.
I solved Android but I don't know how to set in iOS environment.
is iOS has channel like android?
I solved it with the code below in the source receiving Firebase Cloud Messaging (FCM).
// remote message processing function
const sendLocalNotificationWithSound = remoteMessage => {
if (Platform.OS === 'ios') {
PushNotificationIOS.addNotificationRequest({
id: remoteMessage.notification.notificationId
? remoteMessage.notification.notificationId
: new Date().toString(),
title: remoteMessage.notification.title,
subtitle: remoteMessage.notification.message
? remoteMessage.notification.message
: '',
body: remoteMessage.notification.body,
sound: remoteMessage.notification.sound
})
} else {
PushNotification.localNotification({
channelId: remoteMessage.notification.android.channelId,
id: remoteMessage.notification.notificationId
? remoteMessage.notification.notificationId
: new Date().toString(),
title: remoteMessage.notification.title,
message: remoteMessage.notification.body,
soundName: remoteMessage.notification.android.sound,
playSound: true,
smallIcon: 'ic_stat_ic_notification',
color: '#FFFFFF',
largeIcon: '',
largeIconUrl: '',
vibrate: true,
groupSummary: true
})
}
}
// remote message receiving
React.useEffect(() => {
const getMessage = messaging().onMessage(remoteMessage => {
sendLocalNotificationWithSound(remoteMessage)
})
return () => getMessage()
}, [])
First, the react-native-push-notification and #react-native-community/push-notification-ios libraries must be installed.
react-native-push-notification
#react-native-community/push-notification-ios
function messaging() in code is #react-native-firebase/messaging library.
you have to import module like:
import messaging from '#react-native-firebase/messaging'
on head in code
The official documentation for react-native FCM (firebase cloud messaging) is here -> REACT NATIVE FIREBASE
FCM sending side JSON sample file
(I test with postman)
{
"to": "your fcm token here",
"notification": {
"title": "your notification title",
"body": "your notification description",
"sound": "your notification sound name (Runs on iOS)",
"android_channel_id": "your android channel id here (Runs on Android)",
"priority": "high",
"contentAvailable": true,
"mutableContent": 1
},
"data": {
... // if you need data property
}
}

Removing local notification on ios react native

Im developing an app which is connected to a bluetooth device. If the device is disconnected the user receives a push notification that the device is disconnected. This works fine on both android and iOS with this library.
If the application reconnects, then i want to remove the notification. This works fine on android, but does not remove the notification on iOS.
Here is the code for creating notification:
export const pushNotification = (title: string, message: string) => {
PushNotification.localNotification({
channelId: 'channel',
title: title,
message: message,
id: '1',
});
};
export const configurePushNotification = () => {
PushNotification.configure({
requestPermissions: true,
});
};
export const createChannelForPushNotification = () => {
PushNotification.createChannel(
{
channelId: 'channel',
channelName: 'Push Notification Channel',
channelDescription: 'A channel to send push notifications',
soundName: 'default',
importance: Importance.HIGH,
vibrate: true,
},
(created) => console.log(`createChannel returned '${created}'`),
);
};
I use the code below to remove the notification, which works fine on android.
PushNotification.cancelLocalNotification('1');
Any suggestions as to why it is not removing local notification on iOS?

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

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