收到通知问题并打开片段 [英] Receive a notification issue and open a fragment

查看:163
本文介绍了收到通知问题并打开片段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

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.

这是FirebaseMessagingService代码类:

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

在主要活动中:

    @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 。建立()); FirebaseMessagingService类中的

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

所以请有人帮助!

我更新了FirebaseMessagingService类的代码,现在该应用程序不崩溃,无论是运行还是关闭,我都能在两种情况下收到通知。但是如果我单击通知,则什么也没发生

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


推荐答案

尚未在通知中添加小图标

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

  .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

这篇关于收到通知问题并打开片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆