简体   繁体   English

推送通知弹出-Android

[英]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. 我在按钮单击上使用上面的代码以在android上生成推送通知,并且由于我设置了优先级-MAX,因此在我将其滑出屏幕及其然后在通知抽屉中可用。

I've written the same code on my onReceive for my FCM service. 我已经为我的FCM服务在onReceive上编写了相同的代码。 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. 但是,当我从FCM发出通知时,如果该应用程序在后台运行,则不会弹出窗口,并且该通知会直接在通知栏中显示为图标。 Moreover, even the icon specified is not shown. 此外,即使指定的图标也不会显示。 (R.drawable.net) (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. 我需要将通知作为弹出窗口,并且还想知道如何将drawable设置为通知图标。

There are two types of notification in FCM. 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. 1. 通知消息 -当应用程序在后台运行时,这些消息由FCM SDK自动处理,并在应用程序在前台运行时传递给您的接收者。

2. Data messages -which are handled by the client app. 2. 数据消息 -由客户端应用处理。 These are only handles by your receiver. 这些仅是接收者的手柄。

As per your question i think you are using **.Notification messages ** which are High priority messages . 根据您的问题,我认为您正在使用**。Notification消息**,它们是高优先级消息。 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 . 阅读关于FCM消息处理消息

Keep that in mind that when using Notification messages your onReceive() will not get called when your app is in background . 请记住,在使用Notification消息时,当您的应用程序处于后台状态时,不会调用onReceive()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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