ionic2 app not responding when push notification subscribed by topics - push-notification

Im using ionic2 to implement the push notification.(https://firebase.google.com/)
using device registration id, I'm able to receive the notification.but if i subscribe any topics ,on loading of the app the app is crashing(app error on real hardware : unfortunately,push-app is stopped,android 5+)
tried checking issue using chrome console : But not error or exception
(chrome://inspect/#devices)
implementation has been done based on below post:
https://medium.com/#ankushaggarwal/push-notifications-in-ionic-2-658461108c59#.tis93ojyf
platform.ready().then(() => {
--
--
let pushObj = Push.init({
android: {
senderID: "343415024673",
sound: true,
vibrate: true,
clearNotifications: true,
topics: ['/topics/global']
},
ios: {
alert: "true",
badge: true,
sound: 'true'
},
windows: {}
});
});
Thanks in advance

Related

FCM handles IOS notification when app is foreground which is I do not want to

I use fcm and local_notifications for handling push notifications on my Flutter project.
Sending notifications using this payload:
{
token: body.token,
notification = {
title: body.data.title,
body: body.data.body,
},
data: {
data: JSON.stringify(body.data),
}
}
So when notification comes
on app terminated: Notification shows up perfectly for both app(ios - android)
on app background: Notification shows up perfectly for both app(ios - android)
on app foreground:
android app: notification comes like a data notification, fcm is not alerting anything as I want it.
ios app: when the notification comes, fcm is displaying an alert which is I do not want to. And also local_notifications is displaying at the same time.
My problem is fcm handles foreground notifications on ios app. When it works that way I can't disable the notification that should not appear on foreground.
pubspec.yaml:
firebase_messaging: ^10.0.4
flutter_local_notifications: ^8.1.1+1
firebase foreground notification options:
await instance.setForegroundNotificationPresentationOptions(alert: true, badge: true, sound: true);
When I remove firebase foreground notification options or set false all parameters, local_notifications can't display any notifications too.
local_notifications foreground notification options:
instance.resolvePlatformSpecificImplementation<IOSFlutterLocalNotificationsPlugin>()?.requestPermissions(
alert: true,
badge: true,
sound: true,
);
Thanks.
I found the solution:
It was too obvious :D
await instance.setForegroundNotificationPresentationOptions(alert: false, badge: true, sound: true);
alert parameter should be false if you do not want FCM to handle foreground alers.

Flutter - Background notifications not working in iOS

I'm creating a public chat app in Flutter, and I can't receive background notifications on iOS, when I trigger the method admin.messaging().send(payload) from my Cloud Function.
Here's my payload in the Cloud Function :
var payload = {
notification: {
title: `My Title`,
body: `My message`,
},
android: { priority: "high" },
apns: {
payload: {
aps: {
contentAvailable: true,
},
},
headers: {
"apns-push-type": "background",
"apns-priority": "10",
"apns-topic": "io.flutter.plugins.firebase.messaging", // bundle identifier
},
},
topic: `mytopic`,
};
I tried a bunch of different payloads :
I tried notification + data
I tried only data (no notification) (in that case, I have to locally display the notification, but it is not working because the function FirebaseMessaging.onBackgroundMessage is never called)
I tried to change "apns-priority" to 5
I tried using a token instead of a topic
I set up carefully all I need to configure, thanks to the official documentation :
https://firebase.flutter.dev/docs/messaging/usage/
I also checked Github issues, like these ones :
1041, 6112, 5988, 1644, 4300, 4097
What is working :
Background and foreground notifications on Android and iOS, when sent from Firebase Console (in 'Cloud Messaging' section)
Background and foreground notifications on Android only, when sent from Cloud Function
Foreground notifications on iOS, when sent from Cloud Function
What is NOT working :
Background notifications on iOS when sent from Cloud Function
When app is terminated, it does not work as well.
I finally resolved this issue ! 🎉
I simply removed the line "apns-push-type": "background" in my payload.
Now it is working.
It appears that the line "apns-topic" was useless as well.
Here is my final payload :
var payload = {
notification: {
title: `# ${context.params.passion}`,
body: `${newMsg["senderPseudo"]} ${
type == "image" ? "a envoyé une image." : `: ${newMsg["message"]}`
}`,
},
// Set Android priority to "high"
android: {
priority: "high",
},
// Add APNS (Apple) config
apns: {
payload: {
aps: {
contentAvailable: true,
},
},
headers: {
//"apns-push-type": "background", // This line prevents background notifications to be displayed
"apns-priority": "10",
},
},
token: "dnqTQVso60GfnnuOjHv8_e:APA91bElr-K3xkQMdYHX8VMrMZNCYCjO4zJlGseRh25AS_GT7cg9zlOGdQl4KXvr88ypeWjZjrPzrLRHitsQ-JKQK057ZQb_36c_lfsNjHXbYMYI2iS3jV_HGWf7Ene-ZlPvOb0aRr8u"
};

react native push notification doesn't show banner (pop on screen) when app is in background or closed

I'm using react-native-push-notification and followed its documents. it works fine when the app is in the foreground but when the app is in the background and I try to send a notification from firebase console, just shown a small icon of the app in statusbar but doesn't show banner
I tried to add a new channel but still not working
componentDidMount() {
NetInfo.isConnected.addEventListener('connectionChange', this.handleConnectivityChange);
PushNotification.configure({
// (optional) Called when Token is generated (iOS and Android)
onRegister: function(token) {
console.log( 'TOKEN:', token );
},
// (required) Called when a remote or local notification is opened or received
onNotification: function(notification) {
console.log( 'NOTIFICATION:', notification );
Platform.OS === 'ios' ? notification.finish(PushNotificationIOS.FetchResult.NoData) : null;
},
// ANDROID ONLY: GCM or FCM Sender ID (product_number) (optional - not required for local notifications, but is need to receive remote push notifications)
senderID: Variables.GCM_SENDER_ID,
// IOS ONLY (optional): default: all - Permissions to register.
permissions: {
alert: true,
badge: true,
sound: true
},
visibility: 'public',
popInitialNotification: true,
requestPermissions: true,
});
}
try to off the power saving mode battery because is block notification on background

Ionic 3 Push Notification Topic Subscription

I have the following code for push notifications using ionic cloud and Firebase Cloud Messaging.
let topic = "topics/" + this.user.userRole + "/" + this.user.location;
const options:PushOptions = {
android: {
senderID: "XXXXX",
sound: true,
vibrate: true,
//topics: [topic]
},
ios: {
alert: "true",
badge: false,
sound: "true"
},
windows: {}
};
It works, but as soon as I try to subscribe to a specific topic, then the app crashes when running from android.
When I use subscribe, in the manner below:
pushObject.on('registration').subscribe((data:any) => {
console.log("device registered -> ", data);
this.saveToken(data.registrationId);
let topic = "topics/" + this.user.userRole + "/" + this.user.location;
pushObject.subscribe(topic).then((res:any) => {
console.log("subscribed to topic: ", res);
});
});
Again, nothing happens and I'm not receiving any information showing I was able to subscribe to a topic.
How do I accomplish topic subscription?
I open the project in android studio and ran it from there, instead of using ionic-cli.
I saw an IllegalArgumentException regarding invalid topic name.
This answer helped me solve the issue with regards to acceptable characters in a topic name.

Push Notifications in Ionic 2 with the Pub/Sub Model

I am using the phonegap-plugin-push plugin to do Push Notifications in an Ionic 2 Chat App.
I've implemented it according to the documentation, and it works, but only on the same device. i.e. it sends a notification to the phone if you supply its device token.
My issue is for a chat app, I need a PubSub model. So that a user can publish to a topic and another user can subscribe to that topic even if they are on different decices.
Looking at the documentation, it seems to be possible. See android.topics and ios.topics.
android.topics array [] Optional. If the array contains one or more
strings each string will be used to subscribe to a GcmPubSub topic.
So I try the following:
Javascript Client:
publish
let push = Push.init({
android: {
senderID: "xxxxxxxx",
topics: ['foo']
},
ios: {
alert: "true",
badge: false,
sound: "true",
topics: ['foo']
},
windows: {}
});
push.on('registration', (data) => {
// call the Java Server
let promise: Promise<string> = this.notificationService.push(data.registrationId, 'This is a test message from Ionic Chat');
promise.then((data) => {
});
});
Java Server:
try {
System.out.println("NotificationService: device_token: "+device_token);
logger.info("NotificationService: device_token: "+device_token);
// Prepare JSON containing the GCM message content. What to send and where to send.
JSONObject jGcmData = new JSONObject();
JSONObject jData = new JSONObject();
jData.put("title", "This is title");
jData.put("message", message);
jGcmData.put("to", device_token); // <===== is this the problem???? it is the data.registrationId from the client
// What to send in GCM message.
jGcmData.put("data", jData);
// Create connection to send GCM Message request.
URL url = new URL("https://gcm-http.googleapis.com/gcm/send");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestProperty("Authorization", "key=" + API_KEY);
conn.setRequestProperty("Content-Type", "application/json");
conn.setRequestMethod("POST");
conn.setDoOutput(true);
// Send GCM message content.
OutputStream outputStream = conn.getOutputStream();
outputStream.write(jGcmData.toString().getBytes());
} catch (Exception e) {
e.printStackTrace();
}
Javascript Client:
Subscribe
let push = Push.init({
android: {
senderID: "xxxxxxxx",
topics: ['foo']
},
ios: {
alert: "true",
badge: false,
sound: "true",
topics: ['foo']
},
windows: {}
});
push.on('notification', (data) => {
});
It works on the same device, but it doesn't work if the publisher and subscriber are on different devices.
I think the problem may be that even though another user is subscribed to the topic I publish to, it is still only publishing to the data.registrationId (Device Token).
See the following line on the Java Server:
jGcmData.put("to", device_token);
Question
Does anyone know how to make it send to all subscribers on that topic?
I think the answer is here:
"to": "/topics/foo-bar",
But how should this be formatted for multiple topics? (that's just one topic foo-bar)
Need to Change the Java Server to send to the topic instead of the device:
jGcmData.put("to", "/topics/"+topics);
see: https://developers.google.com/cloud-messaging/

Resources