How to start activity when user clicks a notification?

后端 未结 6 1232
梦如初夏
梦如初夏 2020-12-05 11:18

I am attempting to convert some code I found in a tutorial for my own use. Originally, the code launched the system contacts list when the user would click a notification ge

6条回答
  •  醉酒成梦
    2020-12-05 11:38

    check this code

                public class TestActivity extends Activity {
    private static final int UNIQUE_ID = 882;
    
    public static NotificationManager nm;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    
            Intent navigationIntent = new Intent();
    
    
        navigationIntent.setClass(classname.this, MainActivity.class);
    
    
        PendingIntent pi = PendingIntent.getActivity(this, 0, navigationIntent,
                0);
    
        String body = "New notificattion added!!!";
        String title = "Notification";
    
    
        Notification n = new Notification(R.drawable.icon, body,
                System.currentTimeMillis());
    
                //this is for giving number on the notification icon
    
        n.number = Integer.parseInt(responseText);
    
        n.setLatestEventInfo(this, title, body, pi);
        n.defaults = Notification.DEFAULT_ALL;
    
        nm.notify(UNIQUE_ID, n);
    

提交回复
热议问题