【问题标题】:Send data to activity when Firebase notification is clicked in Android在 Android 中单击 Firebase 通知时将数据发送到活动
【发布时间】:2018-09-30 08:22:24
【问题描述】:

在我的应用程序中,我想将 fireBase 用于通知
我想在点击通知时(当应用程序关闭时,我的意思是应用程序是 background)将带有 putExtra 的数据发送到 ma​​inActivity
我写了下面的代码,但是当点击 notification (在应用程序中是背景)但显示 getStringExtranull

MyNotificationManager 类:

public class MyNotificationManager {

    private Context mCtx;
    private Uri soundUri;
    private static MyNotificationManager mInstance;

    public MyNotificationManager(Context context) {
        mCtx = context;
    }

    public static synchronized MyNotificationManager getInstance(Context context) {
        if (mInstance == null) {
            mInstance = new MyNotificationManager(context);
        }
        return mInstance;
    }

    public void displayNotification(String title, String body) {

        soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

        Intent intent = new Intent(mCtx, MainActivity.class);
        intent.putExtra("fcm_notification", "Y");

        PendingIntent pendingIntent = PendingIntent.getActivity(mCtx, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(mCtx, Constants.NOTIF_CHANNEL_ID)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentTitle(title)
                .setSound(soundUri)
                .setAutoCancel(true)
                .setVibrate(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400})
                .setContentText(body)
                .setContentIntent(pendingIntent);

        NotificationManager mNotifyMgr = (NotificationManager) mCtx.getSystemService(NOTIFICATION_SERVICE);

        if (mNotifyMgr != null) {
            mNotifyMgr.notify(1, mBuilder.build());
        }
    }
}

MyFirebaseMessagingService 类:

public class MyFirebaseMessagingService extends FirebaseMessagingService {

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        showNotify(remoteMessage.getFrom(), remoteMessage.getNotification().getBody());
    }

    private void showNotify(String title, String body) {
        MyNotificationManager myNotificationManager = new MyNotificationManager(getApplicationContext());
        //myNotificationManager.displayNotification(title, body);
        myNotificationManager.displayNotification(title, body);
    }
}

MainActivity 类:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
if (checkIntent()) return;
}

@Override
protected void onNewIntent(Intent intent) {
    super.onNewIntent(intent);
    new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            checkIntent();
        }
    }, 1000);
}

private boolean checkIntent() {
    String value = getIntent().getStringExtra("fcm_notification");
    Toast.makeText(context, "" + value, Toast.LENGTH_SHORT).show();

    if (value == null) return false;

    if (value.equals("Y")) {
        startActivity(new Intent(this, LandingActivity.class));
        // open one activity.

    } else if (value.equals("another thing")) {
        // open another activity.
    }

    finish();
    return true;
}

当点击通知时(应用程序为背景),在Toast 中显示 null 消息,用于此行String value = getIntent().getStringExtra("fcm_notification");

我该如何解决?

【问题讨论】:

  • 应用处于后台时,您需要发送data payload 从通知中获取数据。
  • @Yupi,你能帮帮我吗?请用我上面的代码发送给我代码。你能帮帮我吗?
  • 如何发送通知?来自 Firebase 控制台?

标签: java android firebase android-intent android-notifications


【解决方案1】:

从控制台发送通知时,从“高级选项”添加自定义数据:

由于某种原因,firebase 不允许密钥以 fcm 开头。您不能使用fcm_notification。使用不同的密钥。

对于上图,获取key如下:

String value = getIntent().getStringExtra("test_key");

【讨论】:

  • 再次显示 null :( :(
  • 请帮帮我,我真的需要你的帮助,我的兄弟。请
  • 这很奇怪。您是否从 Firebase 控制台添加了自定义数据?您是否已将 fcm_notification 替换为您使用的密钥?
  • 如果应用在后台,onMessageReceived方法中的代码无关紧要。通知由库内部创建。只需在发送通知之前从 FCM 控制台添加自定义数据。
  • 我用以下新代码更改了我的代码。请看那个。在 MyNotificatioManager = Intent intent = new Intent(mCtx, MainActivity.class); intent.putExtra("test_key", keyTest); PendingIntent pendingIntent = PendingIntent.getActivity(mCtx, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); --- 在 MyFirebaseMessagingService = showNotify(remoteMessage.getFrom(), remoteMessage.getNotification().getBody(), remoteMessage.getData().get("test_key")); --- 并在 fireBase 控制台的自定义数据中添加 test_key。再次显示 null .
【解决方案2】:

试试这个:

Intent intent = new Intent(mCtx, MainActivity.class);
intent.putExtra("EXTRA_DATA", title);

得到:

String value = getIntent().getStringExtra("EXTRA_DATA");

最好换个键。此外,您似乎需要从控制台发送数据,然后将数据发送到另一个 Activity

我已经使用当前通知title来检查它是否返回标题。如果返回值,那么,尝试从控制台发送数据。

【讨论】:

    猜你喜欢
    • 2019-10-07
    • 2020-09-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多