收到通知问题并打开片段

时间:2018-09-30 09:53:36

标签: android android-fragments android-notifications android-push-notification

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

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

第二个问题是,当接收者单击通知时,应用程序打开并停留在默认片段的主活动中。 知道我的应用程序中的主要活动包含多个活动。 我已经为用户个人资料创建了一个片段,并且我必须传递用户ID才能打开该片段,因此它将直接发送给发送朋友请求的用户个人资料,但是似乎没有收到用户ID。

这是类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());
}

在主要活动中:

    @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();
            }
        }
    }
}

崩溃错误是:

enter image description here 它指向: mNotifyMgr.notify(mNotificationId,mBuilder.build()); 在FirebaseMessagingService类中。

所以请有人帮忙!


我更新了类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());
}

2 个答案:

答案 0 :(得分:1)

由于您没有在通知中添加小图标,因此出现了错误

  .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);

developeroneSignal

答案 1 :(得分:0)

设置小图标并为此创建通知通道,由于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());