收到通知问题并打开片段

Receive a notification issue and open a fragment

我正在处理应用程序通知,当用户收到好友请求通知并单击通知时,应用程序必须将他重定向到发送请求的用户个人资料。

我面临的不仅仅是一个问题: 首先,如果应用程序运行在后台接收者可以收到通知,但如果应用程序是运行,发送者发送通知后接收者应用程序崩溃并且不显示通知。

第二个问题是,当接收者单击通知时,应用程序打开并停留在默认片段的主要 activity 中。 在我的应用程序中知道 main activity 包含多个。 我为用户个人资料创建了一个片段,我必须传递一个用户 ID 才能打开这个片段,所以它会定向到发送好友请求的用户个人资料,但似乎没有收到用户 ID。

她是 class FirebaseMessagingService 代码:

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

    String notification_title = remoteMessage.getNotification().getTitle();
    String notification_message = remoteMessage.getNotification().getBody();

    String click_action = remoteMessage.getNotification().getClickAction();
    String from_user_id = remoteMessage.getData().get("from_user_id");

    NotificationCompat.Builder mBuilder =
            new NotificationCompat.Builder(this, getString(R.string.default_notification_channel_id))
                    .setContentTitle(notification_title)
                    .setContentText(notification_message);

    Intent intent = new Intent(click_action);
    intent.putExtra("user_id", from_user_id);
    intent.putExtra("From", "notifyFrag");

    PendingIntent pendingIntent =
            PendingIntent.getActivity(this, 0, intent,
                    PendingIntent.FLAG_UPDATE_CURRENT);
    mBuilder.setContentIntent(pendingIntent);

    int mNotificationId = (int) System.currentTimeMillis();
    NotificationManager mNotifyMgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    mNotifyMgr.notify(mNotificationId, mBuilder.build());
}

主要 activity:

    @Override
protected void onNewIntent(Intent intent) {
    super.onNewIntent(intent);
    setIntent(intent);
}

@Override
protected void onResume() {
    super.onResume();

    if(getIntent().getExtras() != null) {

        String from_user_id = getIntent().getStringExtra("user_id");
        String type = getIntent().getStringExtra("From");

        android.support.v4.app.FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();

        if (type != null) {
            switch (type) {
                case "notifyFrag":
                    UserProfileFragment fragment = new UserProfileFragment();
                    transaction.replace(R.id.fragment_container, fragment);

                    Bundle bundle = new Bundle();
                    bundle.putString("user_id", from_user_id);
                    fragment.setArguments(bundle);

                    transaction.commit();
            }
        }
    }
}

崩溃错误是:

它指向: mNotifyMgr.notify(mNotificationId, mBuilder.build()); 在 class FirebaseMessagingService 中。

所以如果有人可以帮助,请!


我更新了 class FirebaseMessagingService 代码,现在应用程序不会崩溃,如果应用程序 运行 或关闭,我可以在这两种情况下收到通知。但是如果我点击通知没有任何反应

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

    NotificationCompat.Builder mBuilder;
    Intent intent;
    PendingIntent pendingIntent;

    final int NOTIFY_ID = (int) System.currentTimeMillis(); // ID of notification
    String notification_id = getString(R.string.default_notification_channel_id); // default_channel_id

    String notification_title = remoteMessage.getNotification().getTitle();
    String notification_message = remoteMessage.getNotification().getBody();

    String click_action = remoteMessage.getNotification().getClickAction();
    String from_user_id = remoteMessage.getData().get("from_user_id");

    if(mNotifyMgr == null){
        mNotifyMgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    }
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        int importance = NotificationManager.IMPORTANCE_HIGH;
        NotificationChannel mChannel = mNotifyMgr.getNotificationChannel(notification_id);

        if (mChannel == null) {
            mChannel = new NotificationChannel(notification_id, notification_title, importance);
            mChannel.enableVibration(true);
            mChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
            mNotifyMgr.createNotificationChannel(mChannel);
        }
        mBuilder = new NotificationCompat.Builder(this, notification_id)
                        .setSmallIcon(R.drawable.logo_ic)
                        .setContentTitle(notification_title)
                        .setContentText(notification_message)
                        .setPriority(NotificationCompat.PRIORITY_DEFAULT);
        intent = new Intent(click_action);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        intent.putExtra("user_id", from_user_id);
        intent.putExtra("From", "notifyFrag");
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

        pendingIntent =
                PendingIntent.getActivity(this, 0, intent,
                        PendingIntent.FLAG_UPDATE_CURRENT);
        mBuilder.setContentIntent(pendingIntent);
    }
    else {
        mBuilder = new NotificationCompat.Builder(this, notification_id)
                .setSmallIcon(R.drawable.logo_ic)
                .setContentTitle(notification_title)
                .setContentText(notification_message)
                .setPriority(NotificationCompat.PRIORITY_DEFAULT);
        intent = new Intent(click_action);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        intent.putExtra("user_id", from_user_id);
        intent.putExtra("From", "notifyFrag");
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

        pendingIntent =
                PendingIntent.getActivity(this, 0, intent,
                        PendingIntent.FLAG_UPDATE_CURRENT);
        mBuilder.setContentIntent(pendingIntent);
    }

    //Set an ID for the notification
    mNotifyMgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    // notificationId is a unique int for each notification that you must define
    mNotifyMgr.notify(NOTIFY_ID, mBuilder.build());
}

您收到错误消息是因为您没有在通知中添加小图标

  .setSmallIcon(R.drawable.your_icon)

更新您的代码

NotificationCompat.Builder mBuilder =
            new NotificationCompat.Builder(this, getString(R.string.default_notification_channel_id))
                    .setContentTitle(notification_title).setSmallIcon(R.drawable.your_icon)
                    .setContentText(notification_message);

developer , oneSignal

设置小图标并为此创建通知通道,因为Android O,通知需要通道

 Intent broadcastIntent = "Your Intent";
    PendingIntent pendingIntent = PendingIntent.getBroadcast(this, notificationId, broadcastIntent,
        PendingIntent.FLAG_ONE_SHOT);
    Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    NotificationManager notificationManager =
        (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
      NotificationChannel channel = new NotificationChannel(String.valueOf(notificationId),
          message,
          NotificationManager.IMPORTANCE_DEFAULT);
      notificationManager.createNotificationChannel(channel);
    }
    NotificationCompat.Builder notificationBuilder =
        new NotificationCompat.Builder(this, String.valueOf(notificationId))
            .setSmallIcon(R.drawable.ic_notification_status)
            .setContentTitle(getString(R.string.app_name))
            .setContentText(message)
            .setAutoCancel(true)
            .setSound(defaultSoundUri);
    notificationBuilder.setContentIntent(pendingIntent);
    notificationManager.notify(notificationId, notificationBuilder.build());