How to disable notifications on background state? - react-native-firebase

I use react-native-firebase/messaging: 6.4.0 and react-native-push-notification: 3.2.1
In background/quit state i get two notifications:
1. Default notification from react-native-firebase
2. Custom notification from react-native-push-notification
my index.js code:
messaging().setBackgroundMessageHandler(async message => {
pushService.showPush(message);
});
How to disable notifications from react-native-firebase/messaging on background state?

Related

How to run a function when a notification is received with Firebase Messaging ^8.0.0-dev.8 in Flutter?

Hello I'm using Flutter to build my app and I need to to show an alert whenever a new notification is received.
I've been using firebase_messaging 7.0.3 but I run into an error with onBackgroundMessage. A quick Google search helped me find out that the error I was getting hasn't been fixed yet. However one of the devs posted an update 20 days ago about a new version of the package which fixed that issue.
The new version removed the old onMessage handlers and introduced new ones.
Now they got new event handlers which return streams, but haven't been able to make them fire by using the
.listen() function. Whenever I receive a notification a get a this: D/FLTFireMsgReceiver(22032): broadcast received for message printed in the console but the code in the .listen() doesn't get executed.
Here is a link to an article on Firebase Flutter that is a guide for using the new version of the package.
Here is my code:
...
FirebaseMessaging.onMessage.listen((event) {
// do something
});
FirebaseMessaging.onMessageOpenedApp.listen((event) {
// do something
});
FirebaseMessaging.onBackgroundMessage((message) {
// do something
return;
}
...
A solution I found to get the events to fire was to always call:
await FirebaseMessaging.instance.getToken();
right after the
await Firebase.initializeApp();
Once I call that, the FirebaseMessaging.onMessage.listen catches the event as expected.
I was getting the same log when my app is in doze mode for the Data notification with high priority.
This is because of some issue in the firebase-messaging plugin.
Firebase_messaging plugin internally uses JobIntentService to process background fcm notifications
JobIntentService has one constraint in Android O or later versions, when running a Job it will be subject to standard JobScheduler policies. The job will not run immediately while the device is in doze mode. (reference link)
The same issue was raised in the firebase_messaging git repository(bug link)
Solution
One Signal(another push notification provider) solved this issue by having a modified version of JobIntentService. (OneSignal Solution)
At a high level, it uses wake locks for high priority fcm notifications to run service even in Android O and above.
Add this Pull Request changes in your ide by editing respective files.
TL;DR
Add this Pull Request changes in your ide by editing respective files.
Send Data notification with High priority.
FCM Payload:
{
"message": {
"token": "fcm_client_token",
"data": {
"title": "Hello",
"body": "Test Message"
},
"android": {
"priority": "high"
}
}
}

Redirect to particular screen when click on remote push notification in react native (App is in background state)

I want to redirect to inbox screen after clicking on push notification on android and iOS
Case 1: App already opened on android. - Successfully Working
Push notification received. On Clicking on notification page redirected to inbox successfully.
NOTIFICATION: {"dealer": "SWRoa0ZhSkN4Y2hQNW1NeWtFdHFvSDNlcXIyNnc1TG5PS1hOZjJoMElHST0=", "finish": [Function finish], "foreground": false, "id": "-1073464079", "message": "hello", "messagetype": "RESPONSE", "subject": "ENQUIRY RESPONSE", "title": “Sample 1”, "userInteraction": true}
PushNotification.configure({
onNotification: function(notification) {
console.log("NOTIFICATION:", notification);
console.log('NOTIFICATION CLICK STATE : --->' + notification.userInteraction);
if(notification.userInteraction === true) {
NavigationService.navigate('Inbox');
}
},
popInitialNotification: true,
});
Case 2: App running in background or closed on android - Failed
Push notification received. On clicking on notification page redirection not working, only app get opened.
No notification data received
Case 3: On IOS in case of app opened or closed. - Failed
Push notification received. On clicking on notification page redirection not working, only app get opened.
No notification data received
I am using react-native-push-notification library for push notification.
https://github.com/zo0r/react-native-push-notification
Maybe it isn't good to promote other packages. However, I suggest looking into react-native-firebase will be good idea. It provides the feature to handle redirection even when the app is not on foreground.

Flutter/Firebase - How to get "heads up" notification on Android?

I have a Flutter application using the firebase-messaging plugin for push notifications.
I register firebase like normal on the client, and I send the fcmToken to the server.
Notifications are created via a python server using aiofcm (which uses firebase's XMPP api). They're created like this:
message = aiofcm.Message(
device_token = t2,
notification = {
"title":notification_title,
"body":notification_body,
"sound":"default",
"tag":link
},
data = {
"click_action": "FLUTTER_NOTIFICATION_CLICK"
},
priority=aiofcm.PRIORITY_HIGH
)
await fcm.send_message(message)
On iOS, notifications pop-up at the top of the screen.
On Android, only the icon shows up in the notification tray - not any of the notification content. This is tested on a Pixel 3 and a OnePlus 6, both running Android P.
Ideally, I would like the notification to be "heads-up" style like this:
Before I was able to accomplish this using data messages and creating the notification programmatically in native android, however I would like to avoid that if possible since data messages don't get delivered on Android if the app is terminated.
To Get heads-up - Notification - Kindly set "alert: true"
Example :
notification = {
"title":notification_title,
"body":notification_body,
"sound":"default",
"alert" : true
"tag":link
},
For heads up notification you will need to use flutter_local_notifications Plugin. It is even recommended in FlutterFire official documentation for notification to show notification in foreground (background notification also supported ofcourse!) and it shows heads up notification by default.
You should check out the firebase.flutter.dev docs it shows how to do this easily with the help of flutter_local_notifiation
you can use background fetch and local notification package for background app running.

Which is the better Push notification system to use with Nativescript?

I'm a Xamarin dev, but I'm exploring Nativescript.
So I've a question.
Which is the better Push notification system to use with Nativescript?
I like to use Visual Studio App Center. Is't nativescript compatible?
Thx.
Nativescript community advises to use https://github.com/EddyVerbruggen/nativescript-plugin-firebase for push notifications. You can use Visual Studio App Center for nativescript cloud builds.
Of course Firebase will be my recommendation too as it's one single plugin for most common requirements that includes Push Notification.
But if you have plans for using other third parties, there are plugins available for Urban Airship, Azure etc., It's also possible to manage your own backend for sending Push Notification.
I would recommend using nativescript worker
https://docs.nativescript.org/core-concepts/multithreading-model
and nativescript-local-notifications plugin
in your ./worker/sendpush.js
require('globals');
var notification = require("nativescript-local-notifications");
onmessage = function(msg){
if(msg.data.success == true)
{
notification.schedule([{
id : 1,
title: 'New message',
badge : 1,
body: data.msg,
smallIcon : 'res://icon'
]).then(function() {
console.log("Notification scheduled");
},function(error) {
console.log("scheduling error: " + error);
})
}
in your main-page.js
var getloc = new Worker("./workers/get-location");
getloc.postMessage({msg : "blabla", success : true});

How to receive the background notification in iOS 10?

Description - Uptil iOS 9, following method gets called
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
But from iOS 10, apple provide new framework i.e userNotification.framework
and new methods to handle i.e
// The method will be called on the delegate only if the application is in the foreground. If the method is not implemented or the handler is not called in a timely manner then the notification will not be presented. The application can choose to have the notification presented as a sound, badge, alert and/or in the notification list. This decision should be based on whether the information in the notification is otherwise visible to the user.
- (void)userNotificationCenter:(UNUserNotificationCenter *)center
willPresentNotification:(UNNotification *)notification
withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler
and
// The method will be called on the delegate when the user responded to the notification by opening the application, dismissing the notification or choosing a UNNotificationAction. The delegate must be set before the application returns from applicationDidFinishLaunching:.
- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler
But Now I am not able to get the notification call back when app is in background and user not clicked on notification.
What I want is when user is in background and if he received any notification then in which method I will get the call back of received notification.
Thanks in advance.
In iOS10, willPresent will be called when app is running foreground. didReceive will be called when user tap the notification while app is running background.
If you want handle notifications when app is running background, you still need to implement didReceiveRemoteNotification...fetchCompletionHandler, and don't forget to add "content-available" to your notification payload. Set it to 1.

Resources