1

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

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

第二个问题是,当接收者点击通知时,应用程序打开并停留在默认片段的主要活动中。知道在我的应用程序中主要活动包含多个。我为用户配置文件创建了一个片段,我必须传递一个用户 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();
            }
        }
    }
}

崩溃错误是:

在此处输入图像描述 它指向: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());
}
4

2 回答 2

0

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

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

开发者oneSignal

于 2018-09-30T11:29:35.987 回答
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());
于 2018-10-01T08:58:24.457 回答