gpt4 book ai didi

java - 将通知点击数据发送到子 Activity

转载 作者:行者123 更新时间:2023-12-01 16:38:26 24 4
gpt4 key购买 nike

我有两个 Activity ,一个称为 SplashActivity,它是启动器和应用程序的主要 Activity ,第二个称为 MainActivity,它是 SplashActivity 的子 Activity ,基于某个事件,我创建了一个通知,当单击通知时我想要MainActivity 打开并额外发送数据。我的问题是我已经尝试了所有出现在我面前的解决方案,但没有任何效果。

这是我当前的代码:

NotificationChannel channel = new NotificationChannel(String.valueOf(12), name, NotificationManager.IMPORTANCE_HIGH);
channel.setDescription(description);
// Register the channel with the system; you can't change the importance
// or other notification behaviors after this
NotificationManagerCompat notificationManagerCompat = NotificationManagerCompat.from(getApplicationContext());
notificationManagerCompat.createNotificationChannel(channel);

// Create an Intent for the activity you want to start
Intent notifyIntent = new Intent(getApplicationContext(), SplashActivity.class);
notifyIntent.putExtra("notify", "notify");
notifyIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
Intent.FLAG_ACTIVITY_SINGLE_TOP
| Intent.FLAG_ACTIVITY_CLEAR_TOP);
// Create the PendingIntent
PendingIntent notifyPendingIntent = PendingIntent.getActivity(
this, 0, notifyIntent, PendingIntent.FLAG_UPDATE_CURRENT
| PendingIntent.FLAG_ONE_SHOT);


NotificationCompat.Builder builder = new NotificationCompat.Builder(this, String.valueOf(12))
.setSmallIcon(R.drawable.logo)
.setContentTitle(title)
.setContentText(body)
.setVibrate(new long[]{0, 100, 1000, 300})
.setAutoCancel(true)
.setContentIntent(notifyPendingIntent)
.setPriority(NotificationCompat.PRIORITY_HIGH);

Notification notification = builder.build();
notification.flags |= Notification.FLAG_AUTO_CANCEL;

// notificationId is a unique int for each notification that you must define
notificationManagerCompat.notify(12, notification);

list 中的两个 Activity 声明:

    <activity
android:name=".home.activities.MainActivity"
android:parentActivityName=".SplashActivity"
android:screenOrientation="portrait"
tools:ignore="LockedOrientationActivity" />

<activity
android:name=".SplashActivity"
android:screenOrientation="portrait"
tools:ignore="LockedOrientationActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

有人有什么解决办法吗?为什么我当前的代码不起作用?

请帮助我,我已经工作了两天才能实现此目的。

最佳答案

启动 MainActivity 时您忘记了 putExtra 到 Intent,您只显示日志。试试这个

        Intent i = new Intent(this, destinationActivity);
i.putExtras(getIntent());
Log.e(TAG, "navigateTo: " + getIntent().getStringExtra("notify"));
startActivity(i);
finish();

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

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