Receive a notification issue and open a fragment

核能气质少年 提交于 2019-12-11 11:55:22

问题


I am working on application notification when a user receives a friend request notification and clicks on the notification the application has to redirect him to the user profile who have sent the request.

I am facing more than a problem: first, if the application running in the background the receiver can receive a notification, but if the application is running, the receiver application crash after the sender sends a notification and it doesn't show the notification.

the second problem is that when the receiver clicks on the notification the application open and stay in the main activity at the default fragment. knowing in my application the main activity contains multiple. I have created a fragment for the user profile, and I have to pass a user id to open this fragment, so it will direct to the user profile who send the friend request, but it seems that there is no user id received.

Her is the class FirebaseMessagingService code:

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

And in the Main 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();
            }
        }
    }
}

The crash error is:

it point to: mNotifyMgr.notify(mNotificationId, mBuilder.build()); in class FirebaseMessagingService.

So please if someone can help!


I updated the class FirebaseMessagingService code and now the app doesn't crash, I am able to receive a notification in both cases if the app is running or close. but if I click in the notification nothing happen

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

回答1:


you are getting error because you haven't added small icon to notification

  .setSmallIcon(R.drawable.your_icon)

update your code

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




回答2:


Set Small Icon and Create notification channel for that, Since Android O, notification needs Channel

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


来源:https://stackoverflow.com/questions/52576771/receive-a-notification-issue-and-open-a-fragment

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!