0

Hi I am using simple firebase notification console to send the push notifications, but when my app is close, I can only get the default style of notification. Can someone show how to make it work with custom style of notifications within the app is closed. This is my onMessageReceived

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {



    Log.d(this.getClass().getSimpleName(), "onMessageReceived");

    String message = remoteMessage.getData().get("image");


    Log.d(this.getClass().getSimpleName(), "From: " + remoteMessage.getFrom());

    //Check if the message contains data
    if(remoteMessage.getData().size()>0){
        Log.d(this.getClass().getSimpleName(), "Message data: " + remoteMessage.getData());
    }

    //Check if the message contains notification
    if(remoteMessage.getNotification() != null){
        Log.dthis.getClass().getSimpleName(), "Message body: "+remoteMessage.getNotification().getBody());
        sendNotification(remoteMessage.getNotification().getBody());
    }
}

and this is my sendnotification

private void sendNotification(String body) {

    Intent intent = new Intent(this, MainActivity.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

    PendingIntent pendingintent = PendingIntent.getActivity(this, 0/*Request code*/, intent, PendingIntent.FLAG_ONE_SHOT);
    //set sound of notification
    Uri notificationSoound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    NotificationCompat.Builder notifiBuilder = new NotificationCompat.Builder(this)
            .setSmallIcon(R.drawable.ic_launcher)
            .setContentTitle("Firebase Cloud Messaging")
            .setContentText(body)
            .setAutoCancel(true)
            .setSound(notificationSoound)
            .setContentIntent(pendingintent);

    NotificationManager noticationmessanger = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
    noticationmessanger.notify(0/*Id of notification*/,notifiBuilder.build());


}
KENdi
  • 7,576
  • 2
  • 16
  • 31
Serg
  • 51
  • 9

4 Answers4

1

With FCM, you can send two types of messages to clients:

Notification messages, sometimes thought of as "display messages." Data messages, which are handled by the client app.

So if you want to handle messages yourself , you need to send only the data attribute as part of the notification from the server. If you add notification attribute , firebase will handle it automatically and you will not be able to customize it.

Dishonered
  • 8,449
  • 9
  • 37
  • 50
0

@Serg I have recently implemented Firebase notifications and faced the same issue. You cannot customize the notification when your app is in background and notification is sent through firebase console. This is because onMessageReceived method of your receiver won't get called. To handle it you would need to send your notification through PostMan(Firebase Rest Client).It will allow you to custommize your notification in both background or foreground. Also, you can use an internal API which could send the notification.

Aditi
  • 389
  • 4
  • 13
  • @Aditi i tested with PostMan but something goes wrong i get this The request was missing an Authentification Key (FCM Token). Please, refer to section "Authentification" of the FCM documentation, at https://firebase.google.com/docs/cloud-messaging/server.

    The request was missing an Authentification Key (FCM Token). Please, refer to section "Authentification" of the FCM documentation, at https://firebase.google.com/docs/cloud-messaging/server.

    – Serg Apr 25 '17 at 10:18
  • i tested all keys from firbase-json file from console CLOUD MESSAGING and old key and new – Serg Apr 25 '17 at 10:19
  • Have u added the registration token in the "to" part of the payload? Can you post your payload – Aditi Apr 25 '17 at 10:21
  • this ?{ "to": "/topics/my_topic", "data": { "my_custom_key" : "my_custom_value", "other_key" : true } } – Serg Apr 25 '17 at 10:27
  • payload is fine.Its working for me.I suppose u haven't added the FCM server key while making request – Aditi Apr 25 '17 at 10:30
  • i test with old key from colsole in firebase cloud messaging and new cod long – Serg Apr 25 '17 at 10:32
  • Does the Authorization header value is like key="server key" from firebase console – Aditi Apr 25 '17 at 10:34
  • i take it from there Your project -> settings -> Project settings -> Cloud messaging -> Server Key – Serg Apr 25 '17 at 10:44
  • The Authorization key is the legacy server key from Firebase Console. – Aditi Apr 25 '17 at 10:47
  • Your project -> settings -> Project settings -> Cloud messaging ->legacy server key. Then in Header you have to pass key as a text along with legacy server key Authorization key=legacy server key – Aditi Apr 25 '17 at 10:53
  • @Aditi can you show example ?i am not sure that i understand what i must do, thanks – Serg Apr 25 '17 at 11:04
  • Serg that's what i have shown you in pic.How more I can give you an example. Sorry for that.. Because your payload is working fine for me. Using my Authorization key – Aditi Apr 25 '17 at 11:05
  • @Aditi in preety then i check json i get this (Unexpected '<') – Serg Apr 25 '17 at 11:07
  • this is how i send POST /fcm/send HTTP/1.1 Host: fcm.googleapis.com Authorization: old kay Content-Type: application/json Cache-Control: no-cache Postman-Token: 3d4fd963-5183-566a-bfae-2b9ff1eb2f94 { "to": "/topics/my_topic", "data": { "my_custom_key" : "my_custom_value", "other_key" : "true" } } – Serg Apr 25 '17 at 11:12
  • this postman -token u dont have to use here is the link which defines what headers u need http://imgur.com/n6pLoIz – Aditi Apr 25 '17 at 11:22
  • now i get another error ((((((((( Unable to get advertising id: java.lang.NullPointerException: Attempt to read from field 'java.lang.String com.google.android.gms.ads.identifier.d.a' on a null object reference: vfs.a(:com.google.android.gms:3757) – Serg Apr 25 '17 at 11:42
  • Please provide the image – Aditi Apr 25 '17 at 11:43
  • Can you provide the error image so that i can look into it – Aditi Apr 25 '17 at 12:04
  • I suppose you made it gr8. – Aditi Apr 25 '17 at 12:06
  • i don't know what is gr8? – Serg Apr 25 '17 at 12:12
  • Serg now check if you send notification through this does your onMessagerecieved gets called – Aditi Apr 25 '17 at 12:13
  • @Serg it should be called whether your app is in backgroudn or foreground when notification is sent through firebase you onMessageReceived gets called. in "to" parameter could you send registered token id obtained in Android code – Aditi Apr 25 '17 at 12:19
  • @Aditi i answered upper – Serg Apr 25 '17 at 12:25
  • @Serg using token does u get Ok or MessageId in PostMan – Aditi Apr 25 '17 at 12:26
  • @Serg take app on debug – Aditi Apr 25 '17 at 12:32
  • @Aditi i didn't get anything, like i have not override the onMessageReceived method – Serg Apr 25 '17 at 12:45
  • i get this Unable to get advertising id: java.lang.NullPointerException: Attempt to read from field 'java.lang.String com.google.android.gms.ads.identifier.d.a' on a null object reference: vfs.a(:com.google.android.gms:3757) – Serg Apr 25 '17 at 13:04
  • @Aditi do you see my mistake? – Serg Apr 25 '17 at 14:52
0

This is my firebase class

public class MyFirebaseMessagingService extends FirebaseMessagingService {

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    Map<String, String> data = remoteMessage.getData();
    String myCustomKey = data.get("my_custom_key");


    Log.d(Constants.TAG_FRAGMENT,"YESSSSSS" +myCustomKey);

    }
}

ither this YESSSSSS i didn't get

this is my raw

{
"to": "/topics/my_topic",
"data": {
    "my_custom_key" : "my_custom_value",
    "other_key" : true
 }

}

Serg
  • 51
  • 9
0

In FirebaseMessagingService

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {

    if (remoteMessage == null)
        return;

    //create notification
    if (remoteMessage.getNotification() != null) {

        // Defualt notification genarate by system
    }

    // Check if message contains a data payload.
    if (remoteMessage.getData().size() > 0) {

        try {

            Map<String, String> data = remoteMessage.getData();
            handleDataMessage(data);
        } catch (Exception e) {
            Log.e(TAG, "Exception: " + e.getMessage());
        }
    }
}


private void handleDataMessage(Map<String, String> data) {

        // code of your custom notification
}

In Poster (For testing)

    {

     "registration_ids" : ["fsdfsa54sdf4asd"], // Your perticular user token id
     "data" : {
         "title" : "Testing",
         "message" : "Hi ",
     }
    }

OR if u are use topic then replace

 "registration_ids" : ["fsdfsa54sdf4asd"], // Your perticular user token id

to

  "to": "/topics/my_topic"

here if you use "notification" then it's handle by android defualt notification tray .So use data-message to handle notification manually everytime you get it. (sorry for my english)

Refer this link for better understanding

Community
  • 1
  • 1
Bhavin Patel
  • 872
  • 13
  • 30