下面的代码应该显示抬头通知,但它没有出现。但是,MessageActivity在没有显示通知的情况下启动。该活动应该在点击通知时开始。
API 23(M)中不会发生此问题。它仅发生在API 16(JELLY_BEAN)中。为什么抬头通知出现在Android M中并且没有出现在android JellyBean上?
提前谢谢。
public class RunAlarm extends BroadcastReceiver{
public static final String ACTION = "br.com.irms.randomalarm.RUN_ALARM_ACTION";
private static final int ID_NOTIFICATION = 1;
@Override
public void onReceive(Context context, Intent intent) {
String source = intent.getStringExtra("source");
if(source.equals("scheduled_alarm")) {
Intent showNotificationIntent = new Intent(context, MessageActivity.class);
String contentTitle = context.getString(R.string.alarm);
String contentText = intent.getStringExtra("hour");
createHeadsUpNotificationWithAction(context, showNotificationIntent, contentTitle, contentText, ID_NOTIFICATION);
} else if(source.equals("dismiss_alarm")) {
Toast.makeText(context,"Alarm dismiss",Toast.LENGTH_SHORT).show();
NotificationManagerCompat nm = NotificationManagerCompat.from(context);
nm.cancel(ID_NOTIFICATION);
} else if(source.equals("snooze_alarm")) {
Toast.makeText(context,"Alarm snooze",Toast.LENGTH_SHORT).show();
NotificationManagerCompat nm = NotificationManagerCompat.from(context);
nm.cancel(ID_NOTIFICATION);
}
}
public void createHeadsUpNotificationWithAction((Context context, Intent intent, String contentTitle, String contentText, int id) {
PendingIntent p = getPendingIntent(context, intent, id);
NotificationCompat.Builder b = new NotificationCompat.Builder(context);
b.setDefaults(NotificationCompat.DEFAULT_ALL);
b.setSmallIcon(R.drawable.ic_alarm_black_24dp);
b.setContentTitle(contentTitle);
b.setContentText(contentText);
b.setContentIntent(p);
b.setAutoCancel(true);
b.setPriority(Notification.PRIORITY_MAX);
b.setFullScreenIntent(p,false);
try {
Intent dismissAlarmIntent = new Intent();
dismissAlarmIntent.setAction(ACTION);
dismissAlarmIntent.putExtra("source","dismiss_alarm");
PendingIntent actionDismissAlarmIntent = null;
actionDismissAlarmIntent = PendingIntent.getBroadcast(
context, IdService.getUniqueId(context), dismissAlarmIntent, 0);
Intent snoozeAlarmIntent = new Intent();
snoozeAlarmIntent.setAction(ACTION);
snoozeAlarmIntent.putExtra("source","snooze_alarm");
PendingIntent actionSnoozeAlarmIntent = null;
actionSnoozeAlarmIntent = PendingIntent.getBroadcast(
context, IdService.getUniqueId(context), snoozeAlarmIntent, 0);
b.addAction(R.drawable.ic_delete_black_24dp, "Pause", actionDismissAlarmIntent);
b.addAction(R.drawable.ic_calendar_black_24dp, "Play", actionSnoozeAlarmIntent);
} catch (IOException e) {
e.printStackTrace();
}
NotificationManagerCompat nm = NotificationManagerCompat.from(context);
nm.notify(id, b.build());
}
private static PendingIntent getPendingIntent(Context context, Intent intent, int id) {
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
stackBuilder.addParentStack(intent.getComponent());
stackBuilder.addNextIntent(intent);
PendingIntent p = stackBuilder.getPendingIntent(id, PendingIntent.FLAG_UPDATE_CURRENT);
return p;
}
}
这是我的androidManifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="br.com.irms.layouttab">
<uses-permission android:name="android.permission.VIBRATE" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name="br.com.irms.randomalarm.MainActivity"
android:launchMode="singleTop"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name="br.com.irms.randomalarm.RunAlarm">
<intent-filter>
<action android:name="br.com.irms.randomalarm.RUN_ALARM_ACTION" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
<activity
android:name="br.com.irms.randomalarm.MessageActivity"
android:label="@string/title_activity_message"
android:parentActivityName="br.com.irms.randomalarm.MainActivity">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="br.com.irms.randomalarm.MainActivity" />
</activity>
</application>
</manifest>
答案 0 :(得分:1)
经过一段时间寻找答案后,我在下面的问题上找到了答案
HeadsUp Notification not displayed on KitKat
简而言之,抬头通知仅在android 5.0中添加。所以,它只是出现在状态栏中,我没有意识到这一点。
答案 1 :(得分:1)
您可以像这样管理通知 例如: - if(currentapiVersion&lt; android.os.Build.VERSION_CODES.HONEYCOMB){
notification = new Notification(icon, text, time);
notification.setLatestEventInfo(this, title, text, contentIntent); // This method is removed from the Android 6.0
notification.flags |= Notification.FLAG_AUTO_CANCEL;
mNM.notify(NOTIFICATION, notification);
} else {
NotificationCompat.Builder builder = new NotificationCompat.Builder(
this);
notification = builder.setContentIntent(contentIntent)
.setSmallIcon(icon).setTicker(text).setWhen(time)
.setAutoCancel(true).setContentTitle(title)
.setContentText(text).build();
mNM.notify(NOTIFICATION, notification);
}