Open application after clicking on Notification

后端 未结 11 2002
南笙
南笙 2020-11-22 13:52

I have a notification in my app with the following code:

//Notification Start

   notificationManager = (NotificationManager) getSystemService(Context.NOTIFI         


        
11条回答
  •  情歌与酒
    2020-11-22 14:38

    public static void notifyUser(Activity activity, String header,
            String message) {
        NotificationManager notificationManager = (NotificationManager) activity
                .getSystemService(Activity.NOTIFICATION_SERVICE);
        Intent notificationIntent = new Intent(
                activity.getApplicationContext(), YourActivityToLaunch.class);
        TaskStackBuilder stackBuilder = TaskStackBuilder.create(activity);
        stackBuilder.addParentStack(YourActivityToLaunch.class);
        stackBuilder.addNextIntent(notificationIntent);
        PendingIntent pIntent = stackBuilder.getPendingIntent(0,
                PendingIntent.FLAG_UPDATE_CURRENT);
        Notification notification = new Notification.Builder(activity)
                .setContentTitle(header)
                .setContentText(message)
                .setDefaults(
                        Notification.DEFAULT_SOUND
                                | Notification.DEFAULT_VIBRATE)
                .setContentIntent(pIntent).setAutoCancel(true)
                .setSmallIcon(drawable.notification_icon).build();
        notificationManager.notify(2, notification);
    }
    

提交回复
热议问题