1

I have implemented firebase successfully. The only thing I am not able to achieve is NotificationCompat.PRIORITY_HIGH. Its working when the app is in foreground. In my PHP code I have added priority and its value to high. Though it seems to be useless for android. In my ios app priority high is working. I am not able to figure out how this can be done. Just for your Info : I am aware of payload data and notifications. :) Thank you.

public class MyFirebaseMessagingService extends FirebaseMessagingService {

private static final String TAG = "MyFirebaseMsgService";
Bitmap bitmap;

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    //Displaying data in log
    //It is optional
    Log.d(TAG, "From: " + remoteMessage.getFrom());
    Log.d(TAG, "Notification Message Title: " + remoteMessage.getNotification().getTitle());
    Log.d(TAG, "Notification Message Body: " + remoteMessage.getNotification().getBody());
    String imageUrl = remoteMessage.getData().get("image");
    bitmap = getBitmapfromUrl(imageUrl);
    //Calling method to generate notification
    sendNotification(remoteMessage.getNotification().getBody(), remoteMessage.getNotification().getTitle());
}


private void sendNotification(String messageBody, String title) {
    Intent intent = new Intent(this, HomeActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent,
            PendingIntent.FLAG_ONE_SHOT);
    Log.e("inside notification", "method");
    Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
            .setSmallIcon(R.drawable.ic_stat_bicycle)
            .setColor(getResources().getColor(R.color.ColorPrimary))
            .setContentTitle(title)
            .setContentText(messageBody)
            .setPriority(NotificationCompat.PRIORITY_HIGH)
            .setAutoCancel(true)
            .setSound(defaultSoundUri)
            .setContentIntent(pendingIntent);
    if (Build.VERSION.SDK_INT >= 19) {
        notificationBuilder.setStyle(new NotificationCompat.BigTextStyle().bigText(messageBody));
         notificationBuilder.setStyle(new NotificationCompat.BigPictureStyle()
                    .bigPicture(img))
    }
    NotificationManager notificationManager =
            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    Random rn = new Random();
    int result = rn.nextInt(100 + 1);
    notificationManager.notify(result, notificationBuilder.build());
}
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Salman Shaikh
  • 589
  • 6
  • 19
  • What do you exactly mean by aware of payload DATA and NOTIFICATION ? Please can you share your json that you are sending to firebase endpoint? – Nishant Dubey Sep 30 '16 at 16:44

1 Answers1

1

Here is my answer from another similar post. The issue is within onMessageReceived() method not just priority.

From your question I can see that you are sending Notification payload as well from server which is the root cause of this problem.


onMessageReceived() method will not be called if the app is in background or killed and if the message sent contains DATA and NOTIFICATION payload both.

When app is not running you will anyway receive notification. But if you want to intercept data via onMessageReceived() then you will have to create a custom app server which will send only DATA payload to fcm endpoint.

something like this:

 {
   "to" : "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
   "data" : {
     "Title" : "Title for Notification",
     "body" : "Notification body can go here",
     "Room" : "Something extra"
   },
 }

Hope this solves your issue. (You can refer to fcm documentation here which says)

When your app is in the background, Android directs notification messages to the system tray. A 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). In these cases, the notification is delivered to the device's system tray, and the data payload is delivered in the extras of the intent of your launcher Activity.

So, onMessageReceived() is never called! If you want to handle stuff manually in onMessageReceived follow the instructions in this answer.

Community
  • 1
  • 1
Nishant Dubey
  • 2,802
  • 1
  • 13
  • 18