我刚刚将我的Android应用targetSdkVersion
升级到28。
我正在使用以下代码在Android应用中创建通知:
@SuppressLint("NewApi")
private void handleCommand(Intent intent) {
if (actION_FOREGROUND.equals(intent.getaction())) {
final YouTubeFile ytf = PlayerDataManager.getInstance().getYouTubeFile();
String nowPlayingTitle = "";
if (ytf != null) {
nowPlayingTitle = ytf.getTitle();
}
String ns = Context.NOTIFICATION_SERVICE;
Notificationmanager mNotificationmanager = (Notificationmanager) getSystemService(ns);
long when = System.currentTimeMillis();
final RemoteViews contentView = new RemoteViews(getPackageName(),R.layout.itube_notification);
ReceiverNotification.s_RemoteView = contentView;
if (ytf != null) {
try {
Bitmap bmp = ImageLoader.getInstance().loadImageSync(ytf.getImageUrl());
contentView.setImageViewBitmap(R.id.icon_fav_notification,bmp);
} catch (Exception e) {
Logger.e(e);
}
}
contentView.setTextViewText(R.id.toptext,nowPlayingTitle);
Intent rewindIntent = new Intent(this,ReceiverNotification.class);
rewindIntent.putExtra("actION",0);
rewindIntent.setaction("rewind");
PendingIntent pendingIntent1 = PendingIntent.getBroadcast(getapplicationContext(),000,rewindIntent,flaG_UPDATE_CURRENT);
contentView.setOnClickPendingIntent(R.id.imageButtonPlayLast,pendingIntent1);
Intent playIntent = new Intent(this,ReceiverNotification.class);
playIntent.putExtra("actION",1);
playIntent.setaction("PLAY");
PendingIntent pendingIntent2 = PendingIntent
.getBroadcast(getapplicationContext(),111,playIntent,flaG_UPDATE_CURRENT);
contentView.setOnClickPendingIntent(R.id.imageButtonPlay,pendingIntent2);
Intent forwardIntent = new Intent(this,ReceiverNotification.class);
forwardIntent.putExtra("actION",2);
forwardIntent.setaction("FORWARD");
PendingIntent pendingIntent3 = PendingIntent.getBroadcast(getapplicationContext(),222,forwardIntent,flaG_UPDATE_CURRENT);
contentView.setOnClickPendingIntent(R.id.imageButtonPlayNext,pendingIntent3);
Intent exitIntent = new Intent(this,ReceiverNotification.class);
exitIntent.putExtra("actION",3);
exitIntent.setaction("EXIT");
PendingIntent pendingIntent4 = PendingIntent
.getBroadcast(getapplicationContext(),333,exitIntent,flaG_UPDATE_CURRENT);
contentView.setOnClickPendingIntent(R.id.exit_icon,pendingIntent4);
contentView.setViewVisibility(R.id.exit_icon,View.VISIBLE);
if (PlayerManager.getInstance().isPlaying()) {
contentView.setImageViewResource(R.id.imageButtonPlay,R.drawable.ic_action_av_pause_light);
} else {
contentView.setImageViewResource(R.id.imageButtonPlay,R.drawable.ic_action_av_play_light);
}
Intent notificationIntent = new Intent(this,Mainactivity.class);
PendingIntent contentIntent = PendingIntent.getactivity(this,notificationIntent,0);
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
createNotificationChannel();
}
NotificationCompat.Builder builder = new NotificationCompat.Builder(GuiManager.getInstance().getMainactivity(),CHANNEL_ID);
builder.setContentIntent(contentIntent)
.setSmallIcon(R.drawable.ic_launcher)
.setWhen(when)
.setautoCancel(false).setVibrate(new long[]{0L})
.setCustomContentView(contentView);
if (Build.VERSION.SDK_INT >= 21) {
builder.setVisibility(Notification.VISIBILITY_PUBLIC);
}
if(Build.VERSION.SDK_INT < Build.VERSION_CODES.O){
builder.setPriority(Notification.PRIORITY_DEFAULT);
}
Notification notification = builder.build();
notification.flags |= Notification.flaG_NO_CLEAR;
notification.defaults |= Notification.DEFAULT_LIGHTS;
if (mNotificationmanager != null) {
mNotificationmanager.notify(NOTIFICATION_ID,notification);
}
startForeground(NOTIFICATION_ID,new Notification());
} else if (actION_BACKGROUND.equals(intent.getaction())) {
stopForegroundCompat(R.string.notification_service_started);
}
}
private void createNotificationChannel() {
// Create the NotificationChannel,but only on API 26+ because
// the NotificationChannel class is new and not in the support library
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
CharSequence name = getString(R.string.notification_channel_title);
String description = getString(R.string.notification_channel_description);
int importance = Notificationmanager.IMPORTANCE_LOW;
NotificationChannel channel = new NotificationChannel(CHANNEL_ID,name,importance);
channel.setDescription(description);
// Register the channel with the system; you can't change the importance
// or other notification behaviors after this
Notificationmanager notificationmanager = getSystemService(Notificationmanager.class);
if (notificationmanager != null) {
notificationmanager.createNotificationChannel(channel);
}
}
}
当我运行它时,我得到这个Exception
:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.****.dev,PID: 17029
android.app.RemoteServiceException: Bad notification for startForeground: java.lang.RuntimeException: invalid channel for service notification: Notification(channel=null pri=0 contentView=null vibrate=null sound=null defaults=0x0 flags=0x40 color=0x00000000 vis=PRIVATE semflags=0x0 semPriority=0 semMissedCount=0)
at android.app.activityThread$H.handleMessage(activityThread.java:1872)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:214)
at android.app.activityThread.main(activityThread.java:7050)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:965)
我已经将其添加到应用清单中
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />