开发者

How do I open the app through clicking the notification bar?

开发者 https://www.devze.com 2022-12-07 21:48 出处:网络
I am trying to open my app by clicking on the app\'s notification bar. The coding is as below. I\'ve tried to add pendingintent but not sure which format it would use.

I am trying to open my app by clicking on the app's notification bar. The coding is as below. I've tried to add pendingintent but not sure which format it would use.

    
        public static final String CHANNEL_ID = "chann开发者_开发技巧el1";
    
        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);
            }
        }
    }


add .setContentIntent(pendingIntent) to your notification builder

val newIntent = Intent(context, MainActivity::class.java)
newIntent.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK

val pendingIntent = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
        PendingIntent.getActivity(context, 0, newIntent, PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE)
    } else {
        PendingIntent.getActivity(context, 0, newIntent, PendingIntent.FLAG_UPDATE_CURRENT)
    }


You can set a Pending Intent for your Notification so that when you click you notification it opens you app:

Android Developers

0

精彩评论

暂无评论...
验证码 换一张
取 消