Open application after clicking on Notification

后端 未结 11 2114
南笙
南笙 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:58

    Here's example using NotificationCompact.Builder class which is the recent version to build notification.

    private void startNotification() {
        Log.i("NextActivity", "startNotification");
    
     // Sets an ID for the notification
          int mNotificationId = 001;
    
        // Build Notification , setOngoing keeps the notification always in status bar
        NotificationCompat.Builder mBuilder =
                new NotificationCompat.Builder(this)
                        .setSmallIcon(R.drawable.ldb)
                        .setContentTitle("Stop LDB")
                        .setContentText("Click to stop LDB")
                        .setOngoing(true);
    
        // Create pending intent, mention the Activity which needs to be 
        //triggered when user clicks on notification(StopScript.class in this case)
    
        PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
                new Intent(this, StopScript.class), PendingIntent.FLAG_UPDATE_CURRENT);
    
    
        mBuilder.setContentIntent(contentIntent);
    
    
        // Gets an instance of the NotificationManager service
       NotificationManager mNotificationManager =
                (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
    
    
        // Builds the notification and issues it.
        mNotificationManager.notify(mNotificationId, mBuilder.build());
    
    
    }
    

提交回复
热议问题