我收到通知,当我点按它时,它只是关闭,应用程序不会重新进入视图。
这是我的MainActivity -
Intent intent = new Intent(getApplicationContext(),NotificationReceiver.class); intent.putExtra(“Message”,notificationText);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 100, intent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.setExact(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
然后NotificationReceiver看起来像这样 -
public class NotificationReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_SINGLE_TOP);
String notificationText = intent.getStringExtra("Message");
//if we want ring on notification then uncomment below line
// Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 100, intent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder = (NotificationCompat.Builder) new NotificationCompat.Builder(context)
.setContentIntent(pendingIntent)
.setSmallIcon(R.drawable.rr)
.setContentTitle("Check your reminders!")
.setContentText(notificationText)
.setAutoCancel(true);
notificationManager.notify(100, builder.build());
}
}
在我的宣言中,我有这个。
<receiver
android:name=".NotificationReceiver" />
我错过了什么?
谢谢!
答案 0 :(得分:2)
您应该创建新的Intent来打开活动,而不是来自onReceive的现有意图。
public class NotificationReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
String notificationText = intent.getStringExtra("Message");
//if we want ring on notification then uncomment below line
// Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
Intent resultIntent = new Intent(context, MainActivity.class);
resultIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 100, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder = (NotificationCompat.Builder) new NotificationCompat.Builder(context)
.setContentIntent(pendingIntent)
.setSmallIcon(R.drawable.rr)
.setContentTitle("Check your reminders!")
.setContentText(notificationText)
.setAutoCancel(true);
notificationManager.notify(100, builder.build());
}
}