用于持久通知的java前台服务
我有一个应用程序是在api级别22中构建的,现在我必须将其更新到api级别26,并且最初构建的持久性通知不再工作。我尝试了stackoverflow的多个代码,但没有成功
我的代码:
void setUpAsForeground(String text) {
Intent mainActivity = new Intent(getApplicationContext(), MainActivity.class);
PendingIntent openMainActivity = PendingIntent.getActivity(getApplicationContext(), 0,
mainActivity, 0);
// Build the notification object.
mNotificationBuilder = new Notification.Builder(getApplicationContext())
.setSmallIcon(R.drawable.radio_icon_128px)
.setTicker(text)
.setWhen(0)
// .setWhen(System.currentTimeMillis())
.setContentTitle(getResources().getString(R.string.app_name) + text)
.setContentText(mMusicProvider.getCurrentSongTitle())
.setContentIntent(openMainActivity)
.setOngoing(true);
startForeground(NOTIFICATION_ID, mNotificationBuilder.build());
// broadcastAction(MainActivity.ACTION_UPDATE_TITLE);
}
任何建议都将不胜感激
# 1 楼答案
在Android 8.0及更高版本上发送通知之前,必须通过传递NotificationChannel实例向系统注册应用程序的通知频道 如果你只需要从你的应用程序中做一个通知,你就可以考虑使用这个代码。p>
# 2 楼答案
从API 26开始,在显示任何通知之前,必须为应用程序提供通知通道。对于Android 7及更低版本,需要设置优先级。有关更多信息和示例,请参阅此文档:https://developer.android.com/training/notify-user/build-notification