【发布时间】:2020-09-06 17:34:55
【问题描述】:
我有两个活动,一个叫做 SplashActivity,它是启动器和应用程序的主要活动,第二个叫做 MainActivity,它是 SplashActivity 的子活动,基于一些事件,我创建了一个通知,当通知单击时我想要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);
清单中的两个活动声明:
<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>
有什么解决办法吗?以及为什么我当前的代码不起作用?
请帮帮我,我已经为此工作了两天。
【问题讨论】:
-
从 SplashActivity 打开 MainActivity 时可以添加代码吗?
-
@CôngHải 我刚刚做了。
标签: java android android-intent android-notifications android-pendingintent