Sending Firebase Push Notifications to Logged-In Users Only

Send notifications only to logged in users' devices via FCM

Firebase Cloud Messaging has no knowledge of users, so any time you want to relate messages to users you will have write code to do this. What you're describing is indeed a common way of doing this: write the token to a database when the user signs in, remove it from that database when they sign out.

Also see:

  • Sending Firebase Push Notifications to Logged-in Users Only
  • How to target a logged user with FCM?

Sending Firebase Push Notifications to Logged-in Users Only

Push notification are not linked with user status. If you can, you should track user state on your server, checking a login token for example, to check if the user is logged in or not. But i think that you can't be completely sure for the user state.

Send notification to specific user firebase in flutter

The above solution works, however, my solution is much simpler, and avoids adding new technologies

I have figured out how send a notification to another device using an in app feature.

First, you will need to import the necessary packages:

firebase_messaging

flutter_local_notifications

Note: you will also use the http package

Also note: to send notifications to another device, you must know the device token of that device. I prefer getting the token and saving it in Firestore or Realtime Database. Here is the code to get the device token.

String? mtoken = " ";

void getToken() async {
await FirebaseMessaging.instance.getToken().then((token) {
setState(() {
mtoken = token;
});
});
}

The token will be saved in mtoken, you can now use this as the token for the coming steps.

The next step is to request permission to send push notifications to your app.

  void requestPermission() async {
FirebaseMessaging messaging = FirebaseMessaging.instance;

NotificationSettings settings = await messaging.requestPermission(
alert: true,
announcement: false,
badge: true,
carPlay: false,
criticalAlert: false,
provisional: false,
sound: true,
);

if (settings.authorizationStatus == AuthorizationStatus.authorized) {
print('User granted permission');
} else if (settings.authorizationStatus ==
AuthorizationStatus.provisional) {
print('User granted provisional permission');
} else {
print('User declined or has not accepted permission');
}
}

(If you get "User declined or has not accepted permission" in your console, try going out of your app, finding the icon in the homescreen, pressing and holding on the app icon, tapping "App Info", tapping "Notifications" and turn on "All [app name] notifications."

You will also need two functions to load a Firebase Cloud Messaging notification and one to listen for a notification.

Code to load a Firebase Cloud Messaging notification:

 void loadFCM() async {
if (!kIsWeb) {
channel = const AndroidNotificationChannel(
'high_importance_channel', // id
'High Importance Notifications', // title
importance: Importance.high,
enableVibration: true,
);

flutterLocalNotificationsPlugin = FlutterLocalNotificationsPlugin();

/// Create an Android Notification Channel.
///
/// We use this channel in the `AndroidManifest.xml` file to override the
/// default FCM channel to enable heads up notifications.
await flutterLocalNotificationsPlugin
.resolvePlatformSpecificImplementation<
AndroidFlutterLocalNotificationsPlugin>()
?.createNotificationChannel(channel);

/// Update the iOS foreground notification presentation options to allow
/// heads up notifications.
await FirebaseMessaging.instance
.setForegroundNotificationPresentationOptions(
alert: true,
badge: true,
sound: true,
);
}
}

And this function to listen for a Firebase Cloud Messaging notifcation.

void listenFCM() async {
FirebaseMessaging.onMessage.listen((RemoteMessage message) {
RemoteNotification? notification = message.notification;
AndroidNotification? android = message.notification?.android;
if (notification != null && android != null && !kIsWeb) {
flutterLocalNotificationsPlugin.show(
notification.hashCode,
notification.title,
notification.body,
NotificationDetails(
android: AndroidNotificationDetails(
channel.id,
channel.name,
// TODO add a proper drawable resource to android, for now using
// one that already exists in example app.
icon: 'launch_background',
),
),
);
}
});
}

You will want to run loadFCM, listenFCM, and requestPermission when the page is initialized.

  void initState() {
super.initState();
requestPermission();
loadFCM();
listenFCM();
}

The next step is to find your Firebase Cloud Messaging API key. This can simply be done by heading to your Firebase project > Project Settings > Cloud Messaging then copy the API key under Cloud Messaging API (Legacy).

When you have your Firebase Cloud Messaging API key, this is the code to display a notification given the notification title, body, and device token to send it to.

  void sendPushMessage(String body, String title, String token) async {
try {
await http.post(
Uri.parse('https://fcm.googleapis.com/fcm/send'),
headers: <String, String>{
'Content-Type': 'application/json',
'Authorization':
'key=REPLACETHISWITHYOURAPIKEY',
},
body: jsonEncode(
<String, dynamic>{
'notification': <String, dynamic>{
'body': body,
'title': title,
},
'priority': 'high',
'data': <String, dynamic>{
'click_action': 'FLUTTER_NOTIFICATION_CLICK',
'id': '1',
'status': 'done'
},
"to": token,
},
),
);
print('done');
} catch (e) {
print("error push notification");
}
}

Now you can call this function like this:

sendPushMessage('Notification Body', 'Notification Title', 'REPLACEWITHDEVICETOKEN');

I hope this helps.

Can I Send notifications to specific users in firebase cloud messaging console

You can send notifications from the console to specific users by following the steps below:

  1. Get the FCM registration tokens for the users' devices. You can check how to get the tokens for the devices depending on the platform here.
  2. Go to the project's Notification Composer.
  3. Enter the notification details i.e notification title(optional),
    notification text, notification image(optional), notification name(optional)
    .
  4. Click Send test message.
  5. Enter the FCM registration tokens.
  6. Click Test.

How to target a logged user with FCM?

What happen if the user logs out and another user logs in as the token is gonna be the same, so it can be a source of errors?

Handle the logout properly.

As the token can expires or not be relevant anymore (the user has changed device or has deleted the app), how can I know which tokens to keep in my database?

You can check it from your server side.



Related Topics



Leave a reply



Submit