我尝试通过点击应用程序的通知栏来打开我的应用程序。代码如下。我尝试添加pendingintent,但不确定它会使用哪种格式。
public static final String CHANNEL_ID = "channel1";
public static final String ACTION_PREVIOUS = "actionprevious";
public static final String ACTION_PLAY = "actionplay";
public static final String ACTION_NEXT = "actionnext";
public static Notification notification;
public static void createNotification(Context context, TrackModel trackModel, int playbutton, int pos, int size){
if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.O){
NotificationManagerCompat notificationManagerCompat = NotificationManagerCompat.from(context);
MediaSessionCompat mediaSessionCompat = new MediaSessionCompat(context, "tag");
Bitmap icon =BitmapFactory.decodeResource(context.getResources(), R.drawable.img1);
notification = new NotificationCompat.Builder(context,CHANNEL_ID)
.setSmallIcon(R.drawable.ic_music)
.setContentTitle(trackModel.getName())
.setLargeIcon(icon)
.setOnlyAlertOnce(true)
.setShowWhen(false)
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.setAutoCancel(true)
.build();
notificationManagerCompat.notify(1, notification);
}
}
}
2条答案
按热度按时间oo7oh9g91#
将 .setContentIntent(pendingIntent) 添加到您的通知生成器
umuewwlo2#
您可以为通知设置待定意向,以便在单击通知时打开应用:
Android Developers