我一直试图打开我的应用程序到我的MainActivity,并加载一个片段,从通知,我似乎不能这样做成功。我最成功的构建似乎做了一切成功,除了实际打开应用程序。应用程序将完全关闭,我将收到通知并点击它。手机的下拉菜单会缩回,表现得好像要打开应用程序,然后又没有打开。当我手动打开应用程序时,应用程序已经创建并成功导航到片段。它就是打不开我的应用。
我将在下面列出相关代码:
清单代码
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.easy.planner">
<uses-permission android:name="android.permission.SCHEDULE_EXACT_ALARM" />
<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:theme="@style/Theme.Planner"
tools:targetApi="31">
<activity
android:name=".MainActivity"
android:exported="true"
android:launchMode="singleInstance" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>=
<receiver
android:name=".ReminderReceiver"
android:exported="true"
android:theme="@android:style/Theme.NoDisplay"/>
</application>
</manifest>
ReminderReceiver中的代码
Intent notificationIntent = new Intent(context, MainActivity.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP & Intent.FLAG_ACTIVITY_SINGLE_TOP);
notificationIntent.putExtra("id", reminder.getId());
final PendingIntent pendingIntent;
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.S){
pendingIntent = PendingIntent.getActivity(context, reminder.getId(), intent, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_MUTABLE);}
else{
pendingIntent = PendingIntent.getActivity(context, reminder.getId(), intent, PendingIntent.FLAG_UPDATE_CURRENT);}
NotificationHelper helper = new NotificationHelper(context);
String title = "title";
String message = "message";
NotificationCompat.Builder builder = helper
.getReminderNotification(context, title, message,pendingIntent, true);
helper.GetManager().notify(reminder.getId(), builder.build());
通知助手
public class NotificationHelper extends ContextWrapper {
public static final String TAG = "Notification Helper";
public static final String REMINDERS_CHANNEL_ID = "id";
public static final String REMINDERS_CHANNEL_NAME = "name";
private NotificationManager manager;
public NotificationHelper(Context base) {
super(base);
CreateChannels(base);
}
public static boolean CreateChannels(Context context)
{
NotificationChannel reminders = new NotificationChannel(REMINDERS_CHANNEL_ID, REMINDERS_CHANNEL_NAME, NotificationManager.IMPORTANCE_HIGH);
reminders.enableLights(true);
reminders.enableVibration(true);
NotificationManager manager = GetManager(context);
manager.createNotificationChannel(reminders);
return true;
}
public NotificationCompat.Builder getReminderNotification(Context context, String title, String message, PendingIntent pendingIntent, boolean autoCancel)
{
return new NotificationCompat.Builder(getApplicationContext(), REMINDERS_CHANNEL_ID)
.setSmallIcon(R.drawable.notification_temp)
.setContentTitle(title)
.setContentText(message)
.setContentIntent(pendingIntent)
.setAutoCancel(autoCancel);
}
我一直在看很多其他的stackoverflow页面,但还没有找到一个解决方案。
你可以更新你的ReminderReceiver查看下面的代码,并检查它是否工作
Intent notificationIntent = new Intent(context, MainActivity.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP &
Intent.FLAG_ACTIVITY_SINGLE_TOP);
notificationIntent.putExtra("id", reminder.getId());
final PendingIntent pendingIntent;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
pendingIntent = PendingIntent.getActivity(context, reminder.getId(), notificationIntent /*update*/ , PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_MUTABLE);
} else {
pendingIntent = PendingIntent.getActivity(context, reminder.getId(), notificationIntent /*update*/ , PendingIntent.FLAG_UPDATE_CURRENT);
}
NotificationHelper helper = new NotificationHelper(context);
String title = "title";
String message = "message";
NotificationCompat.Builder builder = helper
.getReminderNotification(context, title, message, pendingIntent, true);
helper.GetManager().notify(reminder.getId(), builder.build());
我认为你传递错误的意图到你的NotificationBuilder.
希望这有效!!