Open application after clicking on Notification

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

    Please use below code for complete example of simple notification, in this code you can open application after clicking on Notification, it will solve your problem.

    public class AndroidNotifications extends Activity {
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
    
            Button notificationButton = (Button) findViewById(R.id.notificationButton);
    
            notificationButton.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    Timer timer = new Timer();
                    timer.schedule(new TimerTask() {
                        @Override
                        public void run() {
                            // Notification Title and Message
                            Notification("Dipak Keshariya (Android Developer)",
                                    "This is Message from Dipak Keshariya (Android Developer)");
                        }
                    }, 0);
                }
            });
        }
    
        // Notification Function
        private void Notification(String notificationTitle,
                String notificationMessage) {
            NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
            android.app.Notification notification = new android.app.Notification(
                    R.drawable.ic_launcher, "Message from Dipak Keshariya! (Android Developer)",
                    System.currentTimeMillis());
    
            Intent notificationIntent = new Intent(this, AndroidNotifications.class);
            PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,
                    notificationIntent, 0);
    
            notification.setLatestEventInfo(AndroidNotifications.this,
                    notificationTitle, notificationMessage, pendingIntent);
            notificationManager.notify(10001, notification);
        }
    }
    

    And see below link for more information.

    Simple Notification Example

提交回复
热议问题