Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/220.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在Android中单击Firebase通知时向活动发送数据_Java_Android_Firebase_Android Intent_Android Notifications - Fatal编程技术网

Java 在Android中单击Firebase通知时向活动发送数据

Java 在Android中单击Firebase通知时向活动发送数据,java,android,firebase,android-intent,android-notifications,Java,Android,Firebase,Android Intent,Android Notifications,在我的应用程序中,我希望使用fireBase进行通知 我想在单击通知时(当应用程序关闭时,我的意思是应用程序是后台)使用putExtra将数据发送到mainActivity 我写了下面的代码,但是当点击通知时(应用程序中是后台),但是为getStringExtra显示null MyNotificationManager类: public class MyNotificationManager { private Context mCtx; private Uri soundUr

在我的应用程序中,我希望使用fireBase进行通知
我想在单击通知时(当应用程序关闭时,我的意思是应用程序是
后台
)使用
putExtra
将数据发送到mainActivity
我写了下面的代码,但是当点击通知时(应用程序中是后台),但是为
getStringExtra
显示null

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

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());
        }
    }
}
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);
    }
}
main活动类:

@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;
}
Intent intent = new Intent(mCtx, MainActivity.class);
intent.putExtra("EXTRA_DATA", title);
当点击通知(在应用程序上为后台)时,在
Toast
中为此行显示null消息
String value=getIntent().getStringExtra(“fcm_通知”)


我怎样才能修好它

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

由于某些原因,firebase不允许密钥以fcm开头。您不能使用fcm\U通知。使用不同的键

对于上图,按如下方式接收按键:

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

试试这个:

@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;
}
Intent intent = new Intent(mCtx, MainActivity.class);
intent.putExtra("EXTRA_DATA", title);
并获得:

String value = getIntent().getStringExtra("EXTRA_DATA");
最好换一下钥匙。另外,您似乎需要从控制台发送数据,然后将数据发送到另一个
活动


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

当应用程序处于后台时,您需要发送
数据负载
以从通知中获取数据。@Yupi,您能帮助我吗?请将以上代码发送给我。你能帮我吗?你如何发送通知?从Firebase控制台?再次显示空:(:(请帮帮我,我真的需要你的帮助,我的兄弟。这很奇怪。你是否从Firebase控制台添加了自定义数据?你是否用你使用的密钥替换了
fcm_通知
?如果应用程序在后台,方法
onMessageReceived
中的代码无关紧要。通知是由库内部创建的。只要在发送通知之前,从FCM控制台添加自定义数据。我使用下面的新代码更改代码。请参见。在MyNotificationManager=
Intent Intent Intent=new Intent(mCtx,MainActivity.class);Intent.putExtra(“test_key”,keyTest);PendingIntent PendingIntent=PendingIntent.getActivity(mCtx,0,intent,PENDINGENT.FLAG_UPDATE_CURRENT)
---在MyFirebaseMessagingService=
showNotify(remoteMessage.getFrom()、remoteMessage.getNotification().getBody()、remoteMessage.getData().get(“测试密钥”);
---并在fireBase控制台上的自定义数据中添加
test_密钥
。再次显示空值。