java - 在 Android 中单击 Firebase 通知时将数据发送到 Activity

标签 java android firebase android-intent android-notifications

在我的应用程序中,我想使用 fireBase 进行通知
我想在点击通知时(当应用程序关闭时,我的意思是应用程序是background)使用putExtra发送数据到ma​​inActivity
我写了下面的代码,但是当点击notification时(在应用程序中是后台)但是显示 null for getStringExtra !

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

我该如何解决?

最佳答案

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

enter image description here

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

对于上图,接收 key 如下:

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

关于java - 在 Android 中单击 Firebase 通知时将数据发送到 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52576080/

相关文章:

java - 用于开发 JMX 模型 MBean 的 IDE 插件

Android 在 onTextChanged 中调用 setText

java - 无法从Activity获取值到Fragment,NullPointerException

Android StrictMode 总是提示 FileReader

android - 在应用程序关闭时执行挂起的写入

java - PropertyReferenceException 的原因是什么

java - 如何删除 hibernate 泛型中未经检查的分配警告

java - 如果 MySql 或 phpMyAdmin 包含特殊字符,则给出空指针

javascript - 检查 firebase js .forEach 方法是否完成?

Firebase:在 URL 重写中传递 url 参数