Push Notification Works Incorrectly When App Is on Background or Not Running

Push notification works incorrectly when app is on background or not running

With FCM you specify a POST payload to send to https://fcm.googleapis.com/fcm/send. In that payload you can specify a data or a notification key, or both.

If your payload contains only a data key, your app will handle all push messages itself. E.g. they are all delivered to your onMessageReceived handler.

If your payload contains a notification key, your app will handle push messages itself only if your app is active/in the foreground. If it is not (so it's in the background, or closed entirely), FCM handles showing the notification for you by using the values you put into the notification key payload.

Note that notifications sent from a console (like Firebase console), they always include a notification key.

Looks like you want to be handling the FCM messages yourself so you can customize the notification a bit more etc, so it would be better to not include the notification key in the POST payload, so all push messages are delivered to your onMessageReceived.

You can read more about it here:

Advanced messaging options

Downstream message syntax

Push notifications don't work when app is in background

1) Check your certificate and bundle identifier in Firebase.

2) Make sure you send Device Token to Firebase.

- (void)application:(UIApplication *)application
didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {

// For Development
[FIRInstanceID instanceID] setAPNSToken:deviceToken type:FIRInstanceIDAPNSTokenTypeSandbox];

// For Production
[FIRInstanceID instanceID] setAPNSToken:deviceToken type: FIRInstanceIDAPNSTokenTypeProd];

}

You can checkout this Link for more information.

Flutter push notification is working only when app is in background

This is the default behavior of notifications received from the firebase notification service currently. You have to manually write code if you wanna show the notification when your app is in the foreground.

Here's a demo of showing notification in flutter using flutter_local_notifications package.

NOTE: This is a really basic example of showing notifications in flutter using flutter_local_notification package. There's a lot you can configure. For a detailed explanation, visit homepage of this package or read this really good medium article

Step 1: install flutter_local_notifications package in your pubspec.yaml

Step 2: initiate FlutterLocalNotifications in initState():

@override
void initState() {
super.initState();

var initializationSettingsAndroid =
new AndroidInitializationSettings('@mipmap/ic_launcher');
var initializationSettingsIOS = new IOSInitializationSettings();
var initializationSettings = new InitializationSettings(
initializationSettingsAndroid, initializationSettingsIOS);

flutterLocalNotificationsPlugin = new FlutterLocalNotificationsPlugin();
flutterLocalNotificationsPlugin.initialize(initializationSettings,
onSelectNotification: onSelectNotification);
}

Step 3: Create a function to handle click events on the notification. This function will be called when a user will tap on the notification.

Future<dynamic> onSelectNotification(String payload) async {
/*Do whatever you want to do on notification click. In this case, I'll show an alert dialog*/
showDialog(
context: context,
builder: (_) => AlertDialog(
title: Text(payload),
content: Text("Payload: $payload"),
),
);
}

Step 4: Write a function to show notification:

Future<void> _showNotification(
int notificationId,
String notificationTitle,
String notificationContent,
String payload, {
String channelId = '1234',
String channelTitle = 'Android Channel',
String channelDescription = 'Default Android Channel for notifications',
Priority notificationPriority = Priority.High,
Importance notificationImportance = Importance.Max,
}) async {
var androidPlatformChannelSpecifics = new AndroidNotificationDetails(
channelId,
channelTitle,
channelDescription,
playSound: false,
importance: notificationImportance,
priority: notificationPriority,
);
var iOSPlatformChannelSpecifics =
new IOSNotificationDetails(presentSound: false);
var platformChannelSpecifics = new NotificationDetails(
androidPlatformChannelSpecifics, iOSPlatformChannelSpecifics);
await flutterLocalNotificationsPlugin.show(
notificationId,
notificationTitle,
notificationContent,
platformChannelSpecifics,
payload: payload,
);
}

Step 5: Call the _showNotification() function:

_firebaseMessaging.configure(
onMessage: (Map<String, dynamic> message) async {
//print("Message $message");
_showNotification(1234, "GET title FROM message OBJECT", "GET description FROM message OBJECT", "GET PAYLOAD FROM message OBJECT");
return;
}
}

After this, you will be able to show notification even when your app is in the foreground. Hopefully, this will be useful.

Firebase push notification activity is not opening when app is in background?

If you are using firebase push notification

You need to send a click_action attribute ...to open the desired activity when app is in background.

So try ..

 Intent i = new Intent(click_action);

click_action is the intent-filter mapping to the desired activity. click_action is mandatory

instead of the Explicit intent call - explicit intent call in the above case will only work when the app is in foreground..



Related Topics



Leave a reply



Submit