我刚刚将我的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, 0, 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" />