简体   繁体   English

在Android中单击Firebase通知时将数据发送到活动

[英]Send data to activity when Firebase notification is clicked in Android

In my application I want use fireBase for notification . 在我的应用程序中,我想使用fireBase进行通知
I want when click on notification (when app is closed, my mean is app is background ) send data with putExtra to mainActivity . 我想在单击通知时(当应用关闭时,我的意思是应用处于background )将putExtra数据发送到mainActivity
I write below codes, but when click on notification (in app is background) but show me null for getStringExtra ! 我写下面的代码,但是当单击通知时 (在应用程序中是后台),但显示getStringExtranull

MyNotificationManager class : 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 class : 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 class : 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;
}

When click on notification (on app is background) , show me null message in Toast for this line String value = getIntent().getStringExtra("fcm_notification"); 单击通知(在应用程序上为后台)时,在Toast中显示此行的消息String value = getIntent().getStringExtra("fcm_notification");

How can i fix it? 我该如何解决?

When sending notification from console, add the custom data from 'Advanced options': 从控制台发送通知时,请从“高级选项”中添加自定义数据:

在此处输入图片说明

For some reason, firebase doesn't allow the key to start with fcm. 由于某些原因,firebase不允许密钥以fcm开头。 You cannot use fcm_notification . 您不能使用fcm_notification Use a different key. 使用其他密钥。

For above image, receive the key as follows: 对于上图,请按以下方式接收密钥:

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

Try this: 尝试这个:

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

And to get: 并获得:

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

Better to change the key. 最好更改密钥。 Also, it seems like you'll need to send a data from console and then, sending the data to the another Activity . 另外,似乎您需要从控制台发送数据,然后再将数据发送到另一个Activity

I've used the current notification title to check if it returns the title or not.If it returned the value, so, try sending data from the console. 我使用了当前的通知title来检查它是否返回标题。如果返回了值,那么请尝试从控制台发送数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Android - Firebase 通知在点击时打开一个活动 - Android - Firebase Notification open an Activity when clicked 单击带有Firebase的通知时打开通知活动 - Open Notification activity when clicked on Notification with firebase 在Android 4.0版上单击通知时,永远不会启动Activity - Activity is never launched when the notification is clicked on android version 4.0 如何在Android Studio中使用Firebase在新活动中显示通知数据? - how to show the notification data in new activity using firebase in android studio? 单击通知后,打开一个活动 - When a notification's clicked, open an activity "单击通知时停止通知声音android" - Stop Notification sound when Notification clicked android 如何从Firebase推送通知中获取数据并将其显示在Android活动中? - How to get the data from Firebase Push Notification and display it in your Android Activity? 单击另一个活动时如何发送自定义数组列表的列表项的数据? - How to send data of the list item of a custom arraylist when clicked to another activity? 单击按钮时如何将多个复选框值发送到Firebase数据库 - How to send multiple checkbox values to Firebase Database when a Button is clicked Android Firebase - 当存储给某些用户的新数据在数据库中有特定数据时发送通知 - Firebase - send notification when new new data stored to some users have specific data on database
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM