我用Firebase来构建我的项目。
它还将使用FCM(Firebase云消息)。
但是有一个问题。
当应用在后台运行时,我无法处理 FCM(创建自定义通知)。
官方网站教程说,
案例 1:应用前台 ->覆盖"onMessageReceived()"以创建自定义通知。
案例 2:应用背景 -> 系统将直接创建通知。我们不需要也不能做任何事情。因为在这种情况下它不会触发"onMessageReceived()"。
但是,如果在应用程序处于后台时我什么都不做,则无法创建自定义通知。(例如,用户单击通知后,它将弹出一个窗口以显示详细信息。
那么,当应用程序处于后台时,如何使用 FCM 处理通知呢?
有一个坏消息。
Google 更改了 Firebase 源代码版本"com.google.firebase:firebase-messaging:11.6.0"。
HandelIntent现在是"公共最终无效方法"。 这意味着我们不能覆盖它。
如果要使用该解决方案,请将版本更改为"com.google.firebase:firebase-messaging:11.4.2"
试试我的方式。它可以完美地适用于上面的Android 6.0(api级别23)的项目构建版本,我已经尝试过了。
有比官方网站教程更好的方法
官方网站表示,当应用程序在后台运行时,系统将创建通知。所以你不能通过覆盖"onMessageReceived()"来处理它。因为"onMessageReceived()"仅在应用程序处于前台时触发。
但事实并非如此。 实际上,通知(当应用在后台运行时)是由Firebase库创建的。
在我追踪了火力库代码之后。我找到了一个更好的方法。
第 1 步。覆盖 FirebaseMessagingService 中的"handleIntent()"而不是"onMessageReceived()">,
原因:因为该方法将被触发,应用位于前台或后台。因此,我们可以处理 FCM 消息并在这两种情况下创建自定义通知。
@Override
public void handleIntent(Intent intent) {
Log.d( "FCM", "handleIntent ");
}
第 2 步。解析来自 FCM的消息
如何:
如果您不知道您设置的消息的格式。打印它并尝试解析它。
这是基本插图
Bundle bundle = intent.getExtras();
if (bundle != null) {
for (String key : bundle.keySet()) {
Object value = bundle.get(key);
Log.d("FCM", "Key: " + key + " Value: " + value);
}
}
第 2 步。移除应用在后台运行时由 Firebase 库创建的通知
原因:
我们可以创建自定义通知。但是Firebase库创建的通知仍然存在(实际上它是由"super.handleIntent(intent)"创建的。下面有详细的解释。然后我们将有两个通知。这很奇怪。因此,我们必须删除Firebase库创建的通知
(项目构建级别为上面的Android 6.0):
识别我们要删除的通知并获取信息。并使用"notificationManager.cancel()"删除它们。
private void removeFirebaseOrigianlNotificaitons() {
//check notificationManager is available
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
if (notificationManager == null )
return;
//check api level for getActiveNotifications()
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
//if your Build version is less than android 6.0
//we can remove all notifications instead.
//notificationManager.cancelAll();
return;
}
//check there are notifications
StatusBarNotification[] activeNotifications =
notificationManager.getActiveNotifications();
if (activeNotifications == null)
return;
//remove all notification created by library(super.handleIntent(intent))
for (StatusBarNotification tmp : activeNotifications) {
Log.d("FCM StatusBarNotification",
"StatusBarNotification tag/id: " + tmp.getTag() + " / " + tmp.getId());
String tag = tmp.getTag();
int id = tmp.getId();
//trace the library source code, follow the rule to remove it.
if (tag != null && tag.contains("FCM-Notification"))
notificationManager.cancel(tag, id);
}
}
我的整个示例代码:
public class MyFirebaseMessagingService extends FirebaseMessagingService {
private static int notificationCount=0;
@Override
public void handleIntent(Intent intent) {
//add a log, and you'll see the method will be triggered all the time (both foreground and background).
Log.d( "FCM", "handleIntent");
//if you don't know the format of your FCM message,
//just print it out, and you'll know how to parse it
Bundle bundle = intent.getExtras();
if (bundle != null) {
for (String key : bundle.keySet()) {
Object value = bundle.get(key);
Log.d("FCM", "Key: " + key + " Value: " + value);
}
}
//the background notification is created by super method
//but you can't remove the super method.
//the super method do other things, not just creating the notification
super.handleIntent(intent);
//remove the Notificaitons
removeFirebaseOrigianlNotificaitons();
if (bundle ==null)
return;
//pares the message
CloudMsg cloudMsg = parseCloudMsg(bundle);
//if you want take the data to Activity, set it
Bundle myBundle = new Bundle();
myBundle.putSerializable(TYPE_FCM_PLATFORM, cloudMsg);
Intent myIntent = new Intent(this, NotificationActivity.class);
myIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
myIntent.putExtras(myBundle);
PendingIntent pendingIntent = PendingIntent.getActivity(this, notificationCount, myIntent, PendingIntent.FLAG_UPDATE_CURRENT);
//set the Notification
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.icon)
.setContentTitle(cloudMsg.getTitle())
.setContentText(cloudMsg.getMessage())
.setAutoCancel(true)
.setContentIntent(pendingIntent);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(notificationCount++, notificationBuilder.build());
}
/**
* parse the message which is from FCM
* @param bundle
*/
private CloudMsg parseCloudMsg(Bundle bundle) {
String title = null, msg=null;
//if the message is sent from Firebase platform, the key will be that
msg = (String) bundle.get("gcm.notification.body");
if(bundle.containsKey("gcm.notification.title"))
title = (String) bundle.get("gcm.notification.title");
//parse your custom message
String testValue=null;
testValue = (String) bundle.get("testKey");
//package them into a object(CloudMsg is your own structure), it is easy to send to Activity.
CloudMsg cloudMsg = new CloudMsg(title, msg, testValue);
return cloudMsg;
}
/**
* remove the notification created by "super.handleIntent(intent)"
*/
private void removeFirebaseOrigianlNotificaitons() {
//check notificationManager is available
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
if (notificationManager == null )
return;
//check api level for getActiveNotifications()
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
//if your Build version is less than android 6.0
//we can remove all notifications instead.
//notificationManager.cancelAll();
return;
}
//check there are notifications
StatusBarNotification[] activeNotifications =
notificationManager.getActiveNotifications();
if (activeNotifications == null)
return;
//remove all notification created by library(super.handleIntent(intent))
for (StatusBarNotification tmp : activeNotifications) {
Log.d("FCM StatusBarNotification",
"tag/id: " + tmp.getTag() + " / " + tmp.getId());
String tag = tmp.getTag();
int id = tmp.getId();
//trace the library source code, follow the rule to remove it.
if (tag != null && tag.contains("FCM-Notification"))
notificationManager.cancel(tag, id);
}
}
}
但是,如果在应用程序处于后台时我什么都不做,则无法创建自定义通知。(例如,用户单击通知后,它将弹出一个窗口以显示详细信息。
那么,当应用程序处于后台时,如何使用 FCM 处理通知呢?
首先,您需要创建发送到 fcm 服务器的正确消息有效负载。例:
{
"to": "topic_name",
"priority": "high",
"data": {
"field1": "field1 value"
"field2": "field2 value"
}
"notification" : {
"body" : "Lorem ipsum",
"title" : "sampke title"
"click_action": "SHOW_DETAILS"
}
}
data
有效负载是您希望在用户单击通知后显示为消息详细信息的实际数据,notification
有效负载表示生成的通知的外观(可以设置更多属性),您无需自己构建通知,只需在此处设置其属性。
要在用户点击通知后显示您的活动,您需要设置与click_action
对应的意图过滤器:
<intent-filter>
<action android:name="SHOW_DETAILS"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
因此,当用户点击通知时,具有上述意图过滤器的活动将自动启动。 最后一步是在通知点击后启动活动时检索数据。这很容易。自定义数据通过捆绑包传递到活动。在onCreate
活动的方法中,执行以下操作:
Bundle bundle = getIntent().getExtras();
if(bundle.getString("action").equals("SHOW_DETAILS")) /*This indicates activity is launched from notification, not directly*/
{
//Data retrieved from notification payload send
String filed1 = bundle.getString("field1");
String filed2 = bundle.getString("field2");
}
如果应用程序未运行或在后台运行,则上述所有内容均有效。如果你的应用是前台的,则不会创建任何通知。相反,您将收到onMessageReceived()
事件,以便您可以在那里处理相同的数据(我猜您知道如何处理)。
参考:
https://firebase.google.com/docs/cloud-messaging/http-server-ref https://github.com/firebase/quickstart-android/tree/master/messaging
您需要使用 FCM 数据消息才能在 android 应用中创建自定义通知。即使你的应用在后台,也会调用onMessageReceived
,因此你可以处理数据并显示自定义通知。
https://firebase.google.com/docs/cloud-messaging/android/receive
必须从服务器发送的数据消息格式:
{"message":{
"token":"Your Device Token",
"data":{
"Nick" : "Mario",
"body" : "great match!",
"Room" : "PortugalVSDenmark"
}
}
}
FCM如果您的应用被终止,则不会发送后台通知,正如您在有关handleIntent()
解决方案的答案中所述 它可能适用于某些设备和某些旧版本的FCM,此外,如果您@override
Firebase官方文档中未描述的方法,您可能会在这里遇到一些问题, 您自担风险使用它!
解决方案是什么?
您需要在FCM旁边使用自己的推送通知服务,例如Telegram。
或者在GCM旁边使用SyncAdapter,如Gmail。
因此,如果您需要它像这些应用程序一样成功运行,则必须使用自己的技巧。
public class FirebaseMessageReceiver extends FirebaseMessagingService{
private static final String TAG = "main";
String s12;
String channel_id = "general";
Intent intent;
@Override
public void onNewToken(@NonNull String token)
{
Log.d(TAG, "Refreshed token: " + token);
}
@Override
public void
onMessageReceived(RemoteMessage remoteMessage) {
s12=remoteMessage.getNotification().getClickAction();
Log.d("tttt",(remoteMessage.getData().toString()));
Log.d("ttttttt",(remoteMessage.getNotification().toString()));
if (remoteMessage.getNotification() != null) {
showNotification(remoteMessage.getNotification().getTitle(), remoteMessage.getNotification().getBody());
}
//
}
public void handleIntent(Intent intent)
{
try
{
if (intent.getExtras() != null)
{
RemoteMessage.Builder builder = new RemoteMessage.Builder("FirebaseMessageReceiver");
for (String key : intent.getExtras().keySet())
{
builder.addData(key, intent.getExtras().get(key).toString());
}
onMessageReceived(builder.build());
}
else
{
super.handleIntent(intent);
}
}
catch (Exception e)
{
super.handleIntent(intent);
}
}
private RemoteViews getCustomDesign(String title, String message) {
RemoteViews remoteViews = new RemoteViews(getApplicationContext().getPackageName(), R.layout.notification);
remoteViews.setTextViewText(R.id.title111, title);
remoteViews.setTextViewText(R.id.message111, message);
remoteViews.setImageViewResource(R.id.icon111, R.drawable.favicon);
return remoteViews;
}
// Method to display the notifications
public void showNotification(String title, String message) {
intent = new Intent(android.content.Intent.ACTION_VIEW, Uri.parse(s12));
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent notifyIntent = PendingIntent.getActivity(this, 0, intent,
PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE);
Log.d("notifyy",notifyIntent.toString());
NotificationCompat.Builder builder
= new NotificationCompat
.Builder(getApplicationContext(),
channel_id)
.setSmallIcon(R.drawable.favicon)
.setAutoCancel(true)
.setVibrate(new long[]{1000, 1000, 1000, 1000, 1000})
.setOnlyAlertOnce(true)
.setContentIntent(notifyIntent);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
builder = builder.setContent(getCustomDesign(title, message));
}
else {
builder = builder.setContentTitle(title)
.setContentText(message)
.setSmallIcon(R.drawable.favicon);
}
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// Check if the Android Version is greater than Oreo
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel notificationChannel = new NotificationChannel(channel_id, "web_app",
NotificationManager.IMPORTANCE_HIGH);
notificationManager.createNotificationChannel(
notificationChannel);
}
notificationManager.notify(0, builder.build());
}
}