【问题标题】:how to get all firebase push notifications after a connection is established建立连接后如何获取所有firebase推送通知
【发布时间】:2020-06-14 10:04:51
【问题描述】:

服务器正在发送数据(推送)通知,作为客户端,我正在使用 FirebaseMessagingService 和 onMessageRecieved 方法发出通知,它在前台和后台运行良好,但是当我的手机关闭(没有互联网)和建立连接,最后一条通知只发送到安卓手机。

公共类 MyFirebaseMessagingService 扩展 MessagingService {

private static final String TAG = "MyFirebaseMsgService";

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    Map<String, String> data  = remoteMessage.getData();
    String title = remoteMessage.getNotification().getTitle();
    String message = remoteMessage.getNotification().getBody();
    String imageUrl = (String) data.get("image");
    String action = (String) data.get("action");
    Log.i(TAG, "onMessageReceived: title : "+title);
    Log.i(TAG, "onMessageReceived: message : "+message);
    Log.i(TAG, "onMessageReceived: imageUrl : "+imageUrl);
    Log.i(TAG, "onMessageReceived: action : "+action);

    if (remoteMessage.getData().isEmpty()){
        Log.e(TAG , "Empty");
        showNotification(remoteMessage.getNotification().getTitle() , remoteMessage.getNotification().getBody());
    }
    else {
        showNotification(remoteMessage.getData());
    }
}

private void showNotification(Map<String, String> data) {
    String title = data.get("title").toString();
    String body = data.get("body").toString();
    Intent intent = new Intent(this, UserHomeActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
            PendingIntent.FLAG_ONE_SHOT);
    NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    String NOTIFICATION_CHANNEL_ID = "com.example.thelawyerhouse.test";

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
        NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID , "notification"
                , NotificationManager.IMPORTANCE_DEFAULT);
        notificationChannel.setDescription("LawyerHome Channel");
        notificationChannel.enableLights(true);
        notificationManager.createNotificationChannel(notificationChannel);
    }
    NotificationCompat.Builder builder = new NotificationCompat.Builder(this , NOTIFICATION_CHANNEL_ID);
    builder.setAutoCancel(true)
            .setDefaults(Notification.DEFAULT_ALL)
            .setWhen(System.currentTimeMillis())
            .setSmallIcon(R.drawable.ic_notifications_black_24dp)
            .setContentTitle(title).setContentText(body).setContentInfo("info")
            .setContentIntent(pendingIntent);

    notificationManager.notify(new Random().nextInt() , builder.build());
}

private void showNotification(String title, String body) {
    NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    String NOTIFICATION_CHANNEL_ID = "com.example.thelawyerhouse";
    Intent intent = new Intent(this, UserHomeActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
            PendingIntent.FLAG_ONE_SHOT);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
        NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID , "notification"
        , NotificationManager.IMPORTANCE_DEFAULT);
        notificationChannel.setDescription("LawyerHome Channel");
        notificationChannel.enableLights(true);
        notificationManager.createNotificationChannel(notificationChannel);
    }
    NotificationCompat.Builder builder = new NotificationCompat.Builder(this , NOTIFICATION_CHANNEL_ID);
    builder.setAutoCancel(true)
    .setDefaults(Notification.DEFAULT_ALL)
            .setWhen(System.currentTimeMillis())
            .setSmallIcon(R.drawable.ic_notifications_black_24dp)
    .setContentTitle(title).setContentText(body).setContentInfo("info").setContentIntent(pendingIntent);
    notificationManager.notify(new Random().nextInt() , builder.build());
}


@Override
public void onNewToken(@NonNull String s) {
    super.onNewToken(s);
    Log.d("TOKEN" , s);
}

}

【问题讨论】:

    标签: android firebase push-notification firebase-cloud-messaging


    【解决方案1】:

    这似乎是预期的行为,如果您的应用未处于活动状态,则通知将直接发送到系统托盘并且您的 onMessageReceived() 不会被调用

    https://firebase.google.com/docs/cloud-messaging/android/receive

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-04-23
      • 2020-01-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-05
      • 2018-09-19
      • 1970-01-01
      相关资源
      最近更新 更多