简体   繁体   中英

Push Notification pop up - Android

                int mNotificationId = 001;
                Bitmap b = BitmapFactory.decodeResource(getResources(),R.drawable.net);

                NotificationCompat.Builder mBuilder =
                        new NotificationCompat.Builder(MainActivity.this)
                                .setSmallIcon(R.drawable.net)
                                .setLargeIcon(b)
                                .setContentTitle("My notification")
                                .setDefaults(NotificationCompat.DEFAULT_ALL)
                                .setPriority(NotificationCompat.PRIORITY_MAX)
                                .setContentText("Hello World! ");

                NotificationManager mNotifyMgr =
                        (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
                mNotifyMgr.notify(mNotificationId, mBuilder.build());

I'm using the above code on a button click to generate a push notification on android and since I'm setting the priority - MAX, it comes up over the app screen as a pop up before I swipe it out of the screen and its then available in the notification drawer.

I've written the same code on my onReceive for my FCM service. But when I fire notification from FCM and if the app is in the background there is no pop up and the notification is directly shown as an icon in the notification bar. Moreover, even the icon specified is not shown. (R.drawable.net)

I need the notification to be as a popup and was also wondering how I'd get the drawable to be set as the notification icon.

There are two types of notification in FCM.

1. Notification messages - These are handled by the FCM SDK automatically when app is in background, and passed to Your receiver when app is in foreground.

2. Data messages -which are handled by the client app. These are only handles by your receiver.

As per your question i think you are using **.Notification messages ** which are High priority messages . A notification message should formated as :-

{
 "message":{
"token":"bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
"notification":{
  "title":"Portugal vs. Denmark",
  "body":"great match!"
}
}
}

There are some custom parameters available which you can pass from your server to build notification. To add app icon below is the manifest entry.

<meta-data android:name="com.google.firebase.messaging.default_notification_icon"
    android:resource="@drawable/notification_icon" />


 <meta-data    android:name="com.google.firebase.messaging.default_notification_color"
    android:resource="@color/color_blue" />

Use Notification composer to send message. read About FCM Messages and Handling messages .

Keep that in mind that when using Notification messages your onReceive() will not get called when your app is in background .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM