我有两个项目完全相同,一个正在运行API 26目标,另一个正在运行API28。我试图使用警报管理器来触发通知,这在API 26上工作正常,但是当我将其放入API 28时项目无济于事。我的一切都完全一样,所以我不明白为什么这种情况不会出现。有没有人经历过类似的经历?我将附上以下所有代码以及清单。
在定义的10秒钟和25秒钟之后,我收到了日志,因为我不应该收到通知。日志如下:
11-08 14:51:50.963 27292-27292/uareloaded.uareloaded
E/NotificationManager: notifyAsUser: tag=null, id=0,
user=UserHandle{0} 11-08 14:51:55.803
27292-27292/uareloaded.uareloaded V/ActivityThread: Handle window
ActivityRecord{d7e10f1 token=android.os.BinderProxy@2edee5a
{uareloaded.sap.uareloaded/uareloaded.uareloaded.MainActivity}}
visibility: false 11-08 14:52:05.957 27292-27292/uareloaded.uareloaded
E/NotificationManager: notifyAsUser: tag=null, id=0,
user=UserHandle{0}
AlarmReceiver
public class AlarmReceiver extends BroadcastReceiver{
private static final String CHANNEL_ID = "com.singhajit.notificationDemo.channelId";
@Override
public void onReceive(Context context, Intent intent) {
Intent notificationIntent = new Intent(context, NotificationActivity.class);
String passed = intent.getStringExtra("test");
String passed2 = intent.getStringExtra("test2");
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
stackBuilder.addParentStack(NotificationActivity.class);
stackBuilder.addNextIntent(notificationIntent);
String messageBody = "Your event " + passed + " is about to start in 15 minutes, in room "+passed2;
// PendingIntent pendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 999, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Notification.Builder builder = new Notification.Builder(context);
builder.setStyle(new Notification.BigTextStyle(builder)
.bigText(messageBody)
.setBigContentTitle("UA Reloaded Event Starting")
.setSummaryText("Tap To View Info"))
.setContentText(messageBody)
.setSmallIcon(R.drawable.ic_launcher)
.setDefaults(Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE)
.setPriority(Notification.PRIORITY_MAX)
.setVisibility(Notification.VISIBILITY_PUBLIC)
.setContentIntent(pendingIntent);
if (Build.VERSION.SDK_INT >= 21) builder.setVibrate(new long[0]);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
builder.setChannelId(CHANNEL_ID);
}
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
String description = "UA Reloaded Event";
int importance = NotificationManager.IMPORTANCE_HIGH; //Important for heads-up notification
NotificationChannel channel = new NotificationChannel("1", CHANNEL_ID, importance);
channel.setDescription(description);
channel.setShowBadge(true);
channel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
// NotificationManager notificationManager = getSystemService(NotificationManager.class);
notificationManager.createNotificationChannel(channel);
}
notificationManager.notify(0, builder.build());
}
}
MainActivity
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addNotification(10,"eventname","roomname");
addNotification(25,"eventname2","roomname2");
}
public void addNotification(int test, String test2, String test3){
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(this, AlarmReceiver.class);
intent.putExtra("test",test2);
intent.putExtra("test2",test3);
final int _id = 50;
Random random = new Random();
final int randomInt = random.nextInt();
System.out.println("random integer:" + randomInt);
PendingIntent appIntent = PendingIntent.getBroadcast(this, randomInt, intent,PendingIntent.FLAG_UPDATE_CURRENT);
Calendar cal = Calendar.getInstance();
cal.add(Calendar.SECOND, test);
alarmManager.setExact(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), appIntent);
}
}
AndroidManifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="uareloaded.uareloaded">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
<uses-permission android:name="android.permission.ACCESS_NOTIFICATION_POLICY" />
<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"
android:usesCleartextTraffic="true">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".schedule" />
<activity android:name=".Twitter" />
<activity android:name=".Menti" />
<activity android:name=".ScheduleDetail" />
<activity android:name=".Info" />
<activity android:name=".About" />
<activity android:name=".Beacons" />
<activity android:name=".AR" />
<activity android:name=".Favorites" />
<activity android:name=".Exhibitors" />
<activity android:name=".ExhibitorsDetail" />
<activity android:name=".Badges"/>
<activity android:name=".NotificationActivity" />
<receiver android:name=".AlarmReceiver"/>
</application>
</manifest>
答案 0 :(得分:0)
NotificationChannel带有3个参数
您正在创建通知频道,其频道ID为“ 1”
NotificationChannel channel = new NotificationChannel("1", CHANNEL_ID, importance);
但是在设置频道ID时,您将设置为
builder.setChannelId(CHANNEL_ID);
,其中CHANNEL_ID为private static final String CHANNEL_ID = "com.singhajit.notificationDemo.channelId";
使用如下相同的ID创建通知频道:
NotificationChannel channel = new NotificationChannel(CHANNEL_ID, "channelName", importance);
此channelName
将在用户的“应用通知”设置中显示为通知类别。 description
将显示为通知类别的描述。因此,请适当选择channelName
和description
。
有关更多详细信息,请参阅开发者指南here。