Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firebase push notifications update DB

I'm trying to send some data to my users via the new Firebase push notifications service.

I'm adding some "key-value" custom data items with 'Message text' - "dbupdate" to detect if it's an update message or a normal push notification that I want to show my users.

In my receiver I check i there is custom data and run the update, if true. If this isn't the case, I show a normal notification with the text message.

It works only when my application opened. If the application is closed, the notification is shown anyway, and my if check doesn't even running,

some code:

public class PushMessageService extends FirebaseMessagingService {
    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        super.onMessageReceived(remoteMessage);
        Map<String,String> data = remoteMessage.getData();
        String link = data.get("link");
        String name = data.get("name");
        if (link!=null) {
            Log.d("link",link);
            Log.d("name",name);
            UpdateHelper.Go(this, link, name);
        }
        else {
            NotificationCompat.Builder mBuilder =
                    new NotificationCompat.Builder(this)
                            .setSmallIcon(R.drawable.ic_shop_white_24dp)
                            .setAutoCancel(true)
                    .setContentTitle(remoteMessage.getNotification().getTitle())
                   .setContentText(remoteMessage.getNotification().getBody());
            NotificationManager mNotificationManager =
                    (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
            mNotificationManager.notify(5, mBuilder.build());
        }
    }
}

example notficiation here

Why is that so? What can I do to run my code even when the application closed?

like image 480
Nirel Avatar asked Jun 14 '16 10:06

Nirel


People also ask

How do you handle Firebase push notifications?

Handle notification messages in a backgrounded appA user tap on the notification opens the app launcher by default. This includes messages that contain both notification and data payload (and all messages sent from the Notifications console).

Is Firebase good for push notifications?

Firebase Cloud Messaging (FCM) provides a reliable and battery-efficient connection between your server and devices that allows you to deliver and receive messages and notifications on iOS, Android, and the web at no cost.

How does Firebase push notification work internally?

The app server sends messages to the client app: The message is composed, either in the Notifications composer or a trusted environment, and a message request is sent to the FCM backend.

Can FCM notification on Android overwrite previous one?

If specified and a notification with the same tag is already being shown, the new notification replaces the existing one in the notification drawer.


2 Answers

As per my experience with Firebase push:

Problem: When sending push notification through "Firebase console", notification is only received when application is in foreground. If the application is killed or in background, the notification is shown only in Version >= lollipop with incorrect notification icon and/or opening the default launcher activity in the app even if you have defined different Activity in PendingIntent.

As per Firebase docs, a push message is divided into 2 pars, they call them: Docs Link here

  1. notification
  2. data payload

enter image description here

Solution:

There are two ways to send a "Push" message.

1. Firebase Console

Console

2. Messaging API - Image showing a request using Advanced REST Client

Curl Request

Push message is received in onMessageReceived(...) method. Firebase onMessageReceived(...) method will not get called if the app is in background or killed only when the message is sent through Firebase Console.

If you send the message via API, it works fine. Message is delivered in onMessageReceived(...) method whether app is in background, foreground or killed. (Caution: This is working fine in >=Lollipop - Behaviour for Android 4.4 or below is still unpredictable.

Update:

You can send a push message from API (firebase call it downstream msg) like this: https://firebase.google.com/docs/cloud-messaging/downstream

Sending downstream messages from the server

To address or "target" a downstream message, the app server sets to with the receiving client app's registration token. You can send notification messages with predefined fields, or custom data messages; see Notifications and data in the message payload for details on payload support. Examples in this page show how to send data messages in HTTP and XMPP protocols.

HTTP POST Request

https://fcm.googleapis.com/fcm/send
Content-Type:application/json
Authorization:key=AIzaSyZ-1u...0GBYzPu7Udno5aA

{ "data": {
    "score": "5x1",
    "time": "15:10"
  },
  "to" : "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1..."
}
like image 156
Hisham Muneer Avatar answered Oct 01 '22 09:10

Hisham Muneer


When you application is in the foreground, notifications will be passed to FirebaseMessagingService.onMessageReceived(). When the app is not in the foreground, the notification will be handled by the default notification center.

Also see:

  • How to push firebase notification only when CheckBoxPreference is checked?

  • Open app on firebase notification received (FCM)

like image 31
Frank van Puffelen Avatar answered Oct 01 '22 09:10

Frank van Puffelen