Push Notification -Didfinishlaunchingwithoptions

Difference between Push Notifications and In-App Notifications in Facebook Analytics

I found the answer at https://developers.facebook.com/docs/push-notifications/overview#app-events:

Push Notifications

Push notifications generally appear on the lock screen of a device, but they may also appear in a banner or other format, depending on the device. You can add a deep link to a push notification that opens up a specific experience in your app.

In-App Notifications

In-app notifications use rich media—such as photos, animated GIFs, emoji and more—to help you create engaging experiences that people receive directly in your app.

Find a more detailed explanation at https://firebase.google.com/docs/cloud-messaging/concept-options#notifications_and_data_messages. Using the terminology they incorporate in that link, "Notification message" would be what the Facebook documentation calls "Push Notification", and "Data message" would be what in the Facebook documents appears as "In-App Notifications".

Also, at the beginning of this video, the speaker clearly explains what "Push Notifications" and "In-App Notifications" are, even with images in the phone to visualize it. The video is from the official Facebook Developers account, so it is a reliable source: https://www.youtube.com/watch?v=0K7AjHrK4TA.

Discard a push notification when received on device

In OneSignal you want to use setNotificationWillShowInForegroundHandler method to decide whether the need to show a notification to the user or not.

For Android

OneSignal.setNotificationWillShowInForegroundHandler(new NotificationWillShowInForegroundHandler() {
@Override
void notificationWillShowInForeground(OSNotificationReceivedEvent notificationReceivedEvent) {
OSNotification notification = notificationReceivedEvent.getNotification();
// Get custom additional data you sent with the notification
JSONObject data = notification.getAdditionalData();

if (/* some condition */ ) {
// Complete with a notification means it will show
notificationReceivedEvent.complete(notification);
}
else {
// Complete with null means don't show a notification
notificationReceivedEvent.complete(null);
}
}
});

For IOS

let notificationWillShowInForegroundBlock: OSNotificationWillShowInForegroundBlock = { notification, completion in
print("Received Notification: ", notification.notificationId ?? "no id")
print("launchURL: ", notification.launchURL ?? "no launch url")
print("content_available = \(notification.contentAvailable)")

if notification.notificationId == "example_silent_notif" {
// Complete with null means don't show a notification
completion(nil)
} else {
// Complete with a notification means it will show
completion(notification)
}
}
OneSignal.setNotificationWillShowInForegroundHandler(notificationWillShowInForegroundBlock)

Note: For more info, you can check the documentation.

https://documentation.onesignal.com/docs/sdk-notification-event-handlers#foreground-notification-received-event

how to push notification to all users in flutter like

For this you should use topics ,

You need to subscribe users to the all topic and no need for tokens when sending the notification

Based on the publish/subscribe model, FCM topic messaging allows you to send a message to multiple devices that have opted in to a particular topic. You compose topic messages as needed, and FCM handles routing and delivering the message reliably to the right devices.

// subscribe to topic on each app start-up
await FirebaseMessaging.instance.subscribeToTopic('all');

and your data


jsonEncode({
'topic': "all",
'data': {
'via': 'FlutterFire Cloud Messaging!!!',
'count': _messageCount.toString(),
},
'notification': {
'title': 'Hello FlutterFire!',
'body': 'This notification (#$_messageCount) was created via FCM!',
},
});



Difference between the two types of FCM push notification payloads (notification vs data) and when do they arrive to Android FirebaseMessagingService?

(This answer focuses on Android devices)

Firebase Cloud Messaging (FCM) push notifications can be of three types : notification, data and notification+data.

  • Notification messages are meant to be received by the Android operating system itself, with no intervention by the app. When received by Android, they will be shown as a notification in the tray. Some details:

    • The tray notification will not be shown if received when your app is in the foreground.
    • You can implement a FirebaseMessagingService (see the data payload for more info on this), which will receive the message if your app is in the foreground. In your FirebaseMessagingService, you can show a tray notification yourself (or do whatever you want) when you receive the message.
    • When sending the message, you can specify what happens when the user clicks on the notification; this can be controlled by either specifying an activity in the click_action Android-specific option (see this) or by specifying an URL in the link property and having your app configure an intent filter associated with the URL you specified.
  • Data messages are meant to be received by an Android service of your app. This service can, in principle (see below [*]), receive the messages when your app is in the foreground, in the background, or not running at all. Some details:

    • To implement the service, you have to extend FirebaseMessagingService and configure it in your app's manifest.
    • When you receive the message in your FirebaseMessagingService, you can decide to emit a local notification to be shown in the tray. You can do this either when your app is in the background or in the foreground, in principle (see below [*]). Of course, you may also decide to do other stuff instead (or apart) of showing the tray notification.
    • [*] Some phone manufacturers, especially Chinese ones like Xiaomi and Oppo, implement some mechanisms to save battery that include killing services. This means that, by default, your FirebaseMessagingService will not be running on those phones unless your app is on the foreground and, therefore, it will NOT receive your data payloads when your app is not on the foreground. There is no way around this, except if the user whitelists your app specifically. The famous apps like Whatapp or Gmail are by default included in the whitelist, but yours won't; therefore, if you rely on data payloads and you want your app to work on that kind of phones, you'd better guide your user to configure their phone to allow it; here you can see how to do it for Xiaomi (Miui) devices. This can also happen in vanilla Android devices since Android 9 (API level 28) with background restrictions, but the behaviour is opposite: your service won't be killed unless the user requests it; you can check this with ActivityManager.isBackgroundRestricted
  • Notification + data messages include both types of payloads. They behave exactly like notification payload-only messages:

    • When your app is in background, Android shows the notification in the tray. The data payload is accessible to the app if it receives the intent invocation when the user clicks (described above) in intent.extras.
    • When your app is in foreground, your FirebaseMessagingService receives the notification with the contents of the data payload.


Related Topics



Leave a reply



Submit