当应用程序处于后台时,Android Firebase 通知点击不起作用

标签 android

仅在响应中获取消息类型“数据”。但应用程序没有在收到通知时调用 onMessageReceive 方法。

{
    "data" : {
      "body" : "Body of the notification example",
      "action_id" : "4"
    }
  }
}

最佳答案

你做过这样的事吗?这是为了处理数据消息。

public class FirebaseMessaging extends FirebaseMessagingService {

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    super.onMessageReceived(remoteMessage);
    showNotification(remoteMessage.getData());
}

private void showNotification(Map<String, String> data) {
    String title = data.get("title");
    String body = data.get("body");

    NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    String NOTIFICATION_CHANNEL_ID = "ChannelID";

    if(notificationManager != null) {
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel notificationChannel = new NotificationChannel(
                    NOTIFICATION_CHANNEL_ID, "NotificationManager",
                    NotificationManager.IMPORTANCE_DEFAULT);

              notificationChannel.setDescription("My Channel");
              notificationManager.createNotificationChannel(notificationChannel);
        }

        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID);
        notificationBuilder
                .setAutoCancel(true)
                .setDefaults(Notification.DEFAULT_ALL)
                .setWhen(System.currentTimeMillis())
                .setSmallIcon(R.drawable.supportop_icon)
                .setContentTitle(title)
                .setContentText(body);

        // Handle notification click
        PendingIntent intent = PendingIntent.getActivity(this, 0, new Intent(this, Activity.class), 0);
        notificationBuilder.setContentIntent(intent);

        notificationManager.notify(id, notificationBuilder.build());
    }
}
}

关于当应用程序处于后台时,Android Firebase 通知点击不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58237470/

相关文章:

android - 原因:android.view.InflateException:二进制XML文件

Android AVD 屏幕太大

Android - map View Controller

Android Studio 3.2 上的 AndroidX - 运行应用程序时出现 VerifyError

java - Android Studio - 错误 :resource drawable/abc_ic_ab_back_mtrl_am_alpha (aka com. instacoin:drawable/abc_ic_ab_back_mtrl_am_alpha) not found

java - 如何让每次检测的所有iBeacon信息?

android - glsl编程架构哪一部分是 "really"并行执行?

android - 抽屉导航中 fragment 之间的通信

java - 使用 LocationManager 和 GeoCoder 返回 null 的地址

java - 为什么我的循环只输出最后一个结果?