Ionic 5 Capacitor - Register push notification token after change in iOS settings - ios

My code is set up as follows:
export class HomePage implements OnInit {
ngOnInit() {
console.log('Initializing HomePage');
// Request permission to use push notifications
// iOS will prompt user and return if they granted permission or not
// Android will just grant without prompting
PushNotifications.requestPermission().then( result => {
if (result.granted) {
// Register with Apple / Google to receive push via APNS/FCM
PushNotifications.register();
} else {
// Show some error
}
});
// On success, we should be able to receive notifications
PushNotifications.addListener('registration',
(token: PushNotificationToken) => {
alert('Push registration success, token: ' + token.value);
}
);
// Some issue with our setup and push will not work
PushNotifications.addListener('registrationError',
(error: any) => {
alert('Error on registration: ' + JSON.stringify(error));
}
);
// Show us the notification payload if the app is open on our device
PushNotifications.addListener('pushNotificationReceived',
(notification: PushNotification) => {
alert('Push received: ' + JSON.stringify(notification));
}
);
// Method called when tapping on a notification
PushNotifications.addListener('pushNotificationActionPerformed',
(notification: PushNotificationActionPerformed) => {
alert('Push action performed: ' + JSON.stringify(notification));
}
);
}
If the user denies permission on app first launch and then goes into the settings app and enables push notifications and then comes back to the app (without restarting the app) how do I access the registered token in this case? Do I need to register the token each time the app comes into the foreground as well as on app start? What is the recommended approach for an Ionic Capacitor app? Thanks!

I use platform.resume to test scenarios like this. You'll need capacitor3 for this as this was broken in capacitor2.

Related

Firebase Message not on iOS - FIRMessaging received data-message, but FIRMessagingDelegate's-messaging:didReceiveMessage: not implemented

I am working on an ionic app and I have integrated firebase push notifications. On android everything works fine, but on iOS I'm getting below error in the log. I have enabled push notifications capabilities on XCode as well. Any idea why am I getting this error?
FIRMessaging received data-message, but FIRMessagingDelegate's-messaging:didReceiveMessage: not implemented
I got it fixed.
I am using below method to get the token.
// Get permission from the user
async getToken() {
return await new Promise(async (resolve) => {
let token: String;
if (this.platform.is('android')) {
token = await this.firebaseNative.getToken();
console.log('Android device instance id : ', token);
resolve(token);
}
if (this.platform.is('ios')) {
token = await this.firebaseNative.getToken();
await this.firebaseNative.grantPermission();
console.log('iOS device instance id : ', token);
resolve(token);
}
});
}
the issue was with this code line.
await this.firebaseNative.grantPermission();
once it's removed it worked.

react-native-fcm how to redirect on specific page after click on notification from tray

I am trying to redirect on specific page after click on notification from tray in react-native-fcm, but unable to do so. React native fcm documentation does not have proper documentation on handling click listener
I follow this tutorial for react-native-fcm
this.notificationListener = FCM.on(FCMEvent.Notification, notif => {
console.log("Notification recived", notif);
if(notif.local_notification){
console.log("Notification recived local", "local notification");
return;
}
if(notif.opened_from_tray){
console.log("Notification recived tray ", "opened from tray");
return;
}
if(Platform.OS ==='ios'){
//optional
//iOS requires developers to call completionHandler to end notification process. If you do not call it your background remote notifications could be throttled, to read more about it see the above documentation link.
//This library handles it for you automatically with default behavior (for remote notification, finish with NoData; for WillPresent, finish depend on "show_in_foreground"). However if you want to return different result, follow the following code to override
//notif._notificationType is available for iOS platfrom
switch(notif._notificationType){
case NotificationType.Remote:
notif.finish(RemoteNotificationResult.NewData) //other types available: RemoteNotificationResult.NewData, RemoteNotificationResult.ResultFailed
break;
case NotificationType.NotificationResponse:
notif.finish();
break;
case NotificationType.WillPresent:
notif.finish(WillPresentNotificationResult.All) //other types available: WillPresentNotificationResult.None
break;
}
}
Any help would be appreciated.
Thanks in advance
hi everyone problem are fixed by using the setTimeout function in notification opened_from_tray block.
Sample Code
redirect(){ this.props.navigator.push({name:"DetailPage", data:this.state.pageData[1]}); }
`
this.notificationListener = FCM.on(FCMEvent.Notification, notif => {
console.log("Notification recived", notif);
if(notif.local_notification){
console.log("Notification recived local", "local notification");
if(notif.opened_from_tray){
console.log("Notification recived local ", "opened from tray");
setTimeout(() => {
// console.log('setting page call!');
console.log('this is props',props);
this.redirect()
// this.props.navigator.push({name:"Sports"});
// // this.props.navigator.push({name:'DetailPage',data:Data});
}, 5000);
//this.props.navigator.push({name:"Settings"});
}
return;
}
if(notif.opened_from_tray){
console.log("Notification recived tray ", "opened from tray");
//this.props.navigator.replacePrevious({name:"Settings"});
return;
}
if(Platform.OS ==='ios'){
//optional
//iOS requires developers to call completionHandler to end notification process. If you do not call it your background remote notifications could be throttled, to read more about it see the above documentation link.
//This library handles it for you automatically with default behavior (for remote notification, finish with NoData; for WillPresent, finish depend on "show_in_foreground"). However if you want to return different result, follow the following code to override
//notif._notificationType is available for iOS platfrom
switch(notif._notificationType){
case NotificationType.Remote:
notif.finish(RemoteNotificationResult.NewData) //other types available: RemoteNotificationResult.NewData, RemoteNotificationResult.ResultFailed
break;
case NotificationType.NotificationResponse:
notif.finish();
break;
case NotificationType.WillPresent:
notif.finish(WillPresentNotificationResult.All) //other types available: WillPresentNotificationResult.None
break;
}
}`

Titanium: Check iOS notifications upon opening the app without clicking the notification

I am trying to handle push notifications on iOS.
My simple code looks something similar to this:
var Cloud = require("ti.cloud");
var deviceToken = null;
var deviceToken = Ti.App.Properties.getString('deviceToken');
Ti.App.iOS.registerUserNotificationSettings({
types: [
Ti.App.iOS.USER_NOTIFICATION_TYPE_ALERT,
Ti.App.iOS.USER_NOTIFICATION_TYPE_SOUND,
Ti.App.iOS.USER_NOTIFICATION_TYPE_BADGE
]
});
Ti.App.iOS.addEventListener('usernotificationsettings', function registerForPush() {
Ti.App.iOS.removeEventListener('usernotificationsettings', registerForPush);
Ti.Network.registerForPushNotifications({
success: function(e) {
if (e.deviceToken !== Ti.App.Properties.getString('deviceToken', null)) {
deviceToken = e.deviceToken;
Ti.App.Properties.setString('deviceToken', deviceToken)
subscribeToChannel();
} else {
Ti.API.info('Already registered for push notifications!');
}
},
error: function(e) {
Ti.API.error('Failed to register for push notifications: ' + e.error);
},
callback: receivePush
});
});
function subscribeToChannel () {
Cloud.PushNotifications.subscribeToken({
device_token: deviceToken,
channel: 'general',
type: Ti.Platform.name == 'android' ? 'android' : 'ios'
}, function (e) {
alert(e.success === true ? 'Subscribed' : 'Error!');
});
}
// When receieve interactive remote notification
Ti.App.iOS.addEventListener('remotenotificationaction', function(e) {
alert('remotenotificationaction: ' + JSON.stringify(e));
});
// When receieve interactive notification in the background
Ti.App.iOS.addEventListener('localnotificationaction', function(e) {
alert('localnotificationaction');
});
// When receieve interactive notification in the foreground
Ti.App.iOS.addEventListener('notification', function(e) {
alert('notification');
});
function receivePush(e) {
alert('receivePush');
}
For the most part everything works fine. The following happens when I send a remote push notification:
When the app is in the background, a notification appears. Upon clicking the notification, I get the "receivePush" message, as expected
When the app is in the foreground, a notification does not appear, but I still get the "receivePush" message, as expected.
However, when I receive a notification while the app is in the background, and then click on the app directly (i.e. not clicking the notification), none of the above events is triggered!
How can I make sure an event is triggered for the last case.
I don't think this is possible since your callback function is assigned with a notification behavior, not app starting. This is not a Titanium problem but a workflow misunderstanding if you know what I mean.
I think for you is best to always check something when the app starts, not related to notifications.

ionic cloud Push notification does not receiving while sometimes on ios device

I have configured the security profile and install push plugin ,register the push plugin was success
this.push.register().then((t: PushToken) => {
return this.push.saveToken(t);
}).then((t: PushToken) => {
//return success token
console.log(t);
});
And also I am receiving push notification successfully after that i have added following code
//handle push
this.push.rx.notification().subscribe((msg) => {
let postID = msg.payload["item"];
//Goto feed page with arg
this.app.getRootNav().push(FeedPage, {postId:item});
});
Now I am not getting push notification.is there any wrong?

How to open the specified page on ios by receiving a push notification

I have registered the device token
this.push.register().then((t: PushToken) => {
return this.push.saveToken(t);
}).then((t: PushToken) => {
console.log('Token saved:', t.token);
});
And also I am receiving push notification. This is woking fine.
But I want to open a specific view page on push click.
Push handle here:
this.push.rx.notification()
.subscribe((msg) => {
alert(msg.title + ': ' + msg.text);
});
Any idea?

Resources