Ionic 2 push payload - How to use payload in component - android

I'm sending a push notification with Firebase and it works. When I send a payload with the push notification how is possible to read the data from the payload in the component.
When I alert the msg object from the observable it alerts [object, Object]
const cloudSettings: CloudSettings = {
'core': {
'app_id': 'fec59dd8'
},
'push': {
'sender_id': '808269516661',
'pluginConfig': {
'ios': {
'badge': true,
'sound': true
},
'android': {
'iconColor': '#343434',
'senderID': '808269516661'
}
}
}
};
Here is the code that subscribes to the push notification
this.push.rx.notification()
.subscribe((msg) => {
alert(msg.title + ': ' + msg.text);
if (msg.app.asleep || msg.app.closed) {
// The app is being opened from a notification
alert("OPEN APP");
alert(msg);
this.rootPage = RetailPage;
} else {
// The app was already open when the notification was received
alert("APP WAS OPEN");
}
});
}
Your system information:
ordova CLI: 6.3.1
Ionic Framework Version: 2.0.0-rc.3
Ionic CLI Version: 2.1.12
Ionic App Lib Version: 2.1.7
Ionic App Scripts Version: 0.0.46
ios-deploy version: Not installed
ios-sim version: Not installed
OS: Windows 10
Node Version: v6.7.0
Xcode version: Not installed
Any help appreciated
Best regards

Related

OneSignal in Ionic V1 with Android SDK 31+

I'm having problems with my old application built on Ionic V1. From now on we need to use SDK 31 or higher on Android. I made the adjustments in my application but I am not able to make OneSignal work.
Plugin installed: onesignal-cordova-plugin 2.11.3
My app.js:
`
$ionicPlatform.ready(function() {
var notificationOpenedCallback = function(jsonData) {
var data = jsonData.notification.payload.additionalData;
if (data && data.targetUrl) {
if (data.paramsValue) {
$state.go(data.targetUrl, { paramID : data.paramsValue } );
}else{
$state.go(data.targetUrl);
}
}
//alert("Notification opened:\n" + JSON.stringify(jsonData));
console.log('notificationOpenedCallback: ' + JSON.stringify(jsonData));
};
// TODO: Update with your OneSignal AppId before running.
window.plugins.OneSignal
.startInit("MY_ID_ONESIGNAL")
.handleNotificationOpened(notificationOpenedCallback)
.endInit();
window.plugins.OneSignal.getIds(function(ids) {
$localStorage.pushToken = ids.userId;
$rootScope.pushToken = ids.userId;
//alert("player id: " + ids.userId);
});
window.plugins.OneSignal.getPermissionSubscriptionState(function(status) {
idapp = status.subscriptionStatus.userId;
});
`
I tried to update the OneSignal cordova plugin to the latest version but it still doesn't work, it's likely that the app.js needs to be changed.

Error in implementing push notification on Android

I am creating an ionic-angular app. I tried to use push notification with the capacitor plugin. But I got an error like this. Can anyone help me?
**"ERROR Error: Uncaught (in promise): Error: "PushNotifications" plugin is not implemented on android
Error: "PushNotifications" plugin is not implemented on android"**
I resolved "ERROR Error: Uncaught (in promise): Error: "PushNotifications" plugin is not implemented on android
Error: "PushNotifications" plugin is not implemented on android" error please follow below step
Add google-service.json file inside android/app folder
add this code in .ts file
import {
ActionPerformed,
PushNotificationSchema,
PushNotifications,
Token,
} from '#capacitor/push-notifications';
import { Platform } from '#ionic/angular';
constructor(public platform: Platform) {
this.platform.ready().then(() => {
this.pushAdded();
})
}
pushAdded() {
// 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.requestPermissions().then(result => {
if (result.receive === 'granted') {
// Register with Apple / Google to receive push via APNS/FCM
PushNotifications.register();
} else {
// Show some error
}
});
PushNotifications.addListener('registration', (token: Token) => {
alert('Push registration success, token: ' + token.value);
});
PushNotifications.addListener('registrationError', (error: any) => {
alert('Error on registration: ' + JSON.stringify(error));
});
PushNotifications.addListener(
'pushNotificationReceived',
(notification: PushNotificationSchema) => {
alert('Push received: ' + JSON.stringify(notification));
},
);
PushNotifications.addListener(
'pushNotificationActionPerformed',
(notification: ActionPerformed) => {
alert('Push action performed: ' + JSON.stringify(notification));
},
);
}
I have fixed this issue by removing the android folder and re-add that. Then it worked. Thanks, Ravi Ashara for helping.
Check your MainActivity.java within your project. In the last version of Capacitor you have to remove the code "onCreate".
See: https://capacitorjs.com/docs/updating/3-0#switch-to-automatic-android-plugin-loading

Ionic 5 Firebase notification - this.fcm.onNotification().subscribe() never fired

I'm trying to add notification with firebase in my ionic application (ionic 5). I follow this tutorial : https://www.positronx.io/ionic-firebase-fcm-push-notification-tutorial-with-example/
I recieve the token and when i send a notification from firebase console, the notification is displayed on the phone (emulator) but nothing is displayed in the console to handle the notification click...
I add FCM into provides in app.modules.ts and i have added the following code in the app.component.ts
import { FCM } from "#ionic-native/fcm/ngx";
...
constructor(
...
private fcm: FCM
) {}
initializeApp() {
this.platform.ready().then(() => {
this.statusBar.styleDefault();
this.splashScreen.hide();
this.fcm.getToken().then(token => {
console.log(token);
});
this.fcm.onTokenRefresh().subscribe(token => {
console.log(token);
});
this.fcm.onNotification().subscribe(data => {
console.log(data);
if (data.wasTapped) {
console.log('Received in background');
} else {
console.log('Received in foreground');
}
});
if (token === null) {
this.msgService.presentToast(
"Impossible de configurer la reception des notifications"
);
}
// Observer.hasTokenFCM.next(token);
if (this.platform.is("ios") || this.platform.is("android")) {
// this.saveToken(token);
}
});
And a have installed the following plugins:
ionic cordova plugin add cordova-plugin-fcm-with-dependecy-updated
npm install #ionic-native/fcm
cordova plugin list:
cordova-plugin-fcm-with-dependecy-updated 4.4.0 "Cordova FCM Push Plugin"
In my package.json:
dependencies:
"#ionic-native/fcm": "^5.22.0",
"cordova-plugin-fcm-with-dependecy-updated": "^4.1.1",
"cordova" -> "plugins":
"cordova-plugin-fcm-with-dependecy-updated": {
"FCM_CORE_VERSION": "16.0.8",
"FCM_VERSION": "18.0.0",
"GRADLE_TOOLS_VERSION": "2.3.+",
"GOOGLE_SERVICES_VERSION": "3.0.0"
},
Thanks a lot if you found why the "this.fcm.onNotification.subscribe" is never fired...
To fix this, use cordova-plugin-fcm-with-dependecy-updated plugin instead of #ionic-native/fcm/ngx, they are conflicting with each other. If onNotification method is not fired and you can't get the payload, try to use getInitialPushPayload
...
const pushPayload = await this.fcm.getInitialPushPayload();
if (pushPayload) {
this.processPayload(pushPayload);
}
this.fcm.onNotification().subscribe(
(payload: PushNotification) => this.processPayload(payload)
);
...
apparently i need to use
this.firebase.onMessageReceived().subscribe(notification => {
if (notification["tap"]) {
...
}
....
});
with firebase plugin to handle notification... but i don't know why.

ionic configuring push notifications

I'm trying to setup push notifications, I'm stuck here, and I have no idea where to put this code:
const cloudSettings: CloudSettings = {
'core': {
'app_id': 'APP_ID',
},
'push': {
'sender_id': 'SENDER_ID',
'pluginConfig': {
'ios': {
'badge': true,
'sound': true
},
'android': {
'iconColor': '#343434'
}
}
}
};
P.s app.modules.ts doesn't exist in my project
Ionic version: 2.1.4
Cordova: 6.4.0
Try this
http://tphangout.com/ionic-2-push-notifications/
Or you can try wiht OneSignal Push Notfications, they have great documentation...

Error handling on react-native-fcm

I'm integrating FCM on my react-native application using https://github.com/evollu/react-native-fcm.
The notification works but I got this error every time.
console.error: "Notification handler err", {"line":67974,"column":14,"sourceURL":"http://localhost:8081/index.android.bundle?platform=android&dev=true&hot=false&minify=false"}
Environment:
react-native-cli: 2.0.1
react-native: 0.40.0
react-native-fcm: 6.1.0
I'm testing on Android ver 6.0.1 and the app is running in foreground.
I removed old code Firebase initialisation code on index.js, and it caused the error.
firebase.initializeApp({
apiKey: "",
authDomain: "",
databaseURL: "",
storageBucket: ""
});
I removed it and it's working now.
For react-native fcm. See object aps.alert
FCM.on("FCMNotificationReceived", (notification) => {
if (Platform.OS === 'ios') {
if (notification && notification.aps) {
const localNotification = {
title: notification.aps.alert.title,
body: notification.aps.alert.body,
show_in_foreground: true,
}
FCM.presentLocalNotification(localNotification);
}
}
});
FCM.on("FCMNotificationReceived", (notification) => {
// Also trigged when FCM.presentLocalNotification() and callback a notification without **aps** object;
// So the app crash, error
// Just check if (notification && notification.aps) and make localNotification object like above code
})

Categories

Resources