gpt4 book ai didi

在 Android 中单击通知时的 Android getExtras()

转载 作者:行者123 更新时间:2023-12-05 00:06:57 24 4
gpt4 key购买 nike

我有一个严重的问题。在我的 Android 应用程序中,我有这段代码:

类:MessagingService.class

public void onMessageReceived(RemoteMessage remoteMessage) {
String title = remoteMessage.getNotification().getTitle();
String messaggio =remoteMessage.getNotification().getBody();
String data = remoteMessage.getData().get("json");

JSON json = new JSON();
String fragment = json.getNotificaFragment(data);

Log.d("MessagingService","Fragment Ricevuto: " + fragment);


int requestID = (int) System.currentTimeMillis();


Intent intent = new Intent(this, LoginActivity.class);
intent.putExtra("fragment", fragment); //Put your id to your next Intent
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, requestID, intent, PendingIntent.FLAG_ONE_SHOT);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this);
notificationBuilder.setContentTitle(title);
notificationBuilder.setContentText(messaggio);
notificationBuilder.setSmallIcon(R.mipmap.ic_launcher);
notificationBuilder.setVibrate(new long[] { 500, 500 });
notificationBuilder.setSound(Settings.System.DEFAULT_NOTIFICATION_URI);
notificationBuilder.setAutoCancel(true);
notificationBuilder.setContentIntent(pendingIntent);
notificationBuilder.setDefaults(Notification.DEFAULT_VIBRATE | Notification.DEFAULT_SOUND | Notification.FLAG_SHOW_LIGHTS);


NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, notificationBuilder.build());

super.onMessageReceived(remoteMessage);

类:LoginActivity.class

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
Log.d("APP", "AVVIO APPLICAZIONE");

String fragmentDaAprire = getIntent().getStringExtra("fragment");
Log.e("Bundle","Fragment da Aprire: " + fragmentDaAprire);

问题是:

如果我收到通知并且我的应用程序已打开,则结果是正确的:

    01-15 15:09:12.301 11419-11419/livio.***E/Bundle: Fragment da Aprire: messaggi

如果我收到通知并且我的应用程序已关闭,当我单击该通知时结果不正确:(

   01-15 15:09:12.301 11419-11419/livio.*** E/Bundle: Fragment da Aprire: null

感谢帮助

最佳答案

您应该检查此语句 String fragment = json.getNotificaFragment(data);。它没有正确填充。

关于在 Android 中单击通知时的 Android getExtras(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41662264/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com