How to write a notification that does absolutly nothing when clicked?

老子叫甜甜 提交于 2020-07-29 11:15:10

问题


Ive seen many examples of how to make something happen when the user clicks on a notification but I actually don't want anything to happen. If the user clicks on the notification I want the notification to simply dissapear and the user NOT taken anywhere.

In my code below, while FLAG_AUTO_CANCEL clears the notification from the status bar, when the user clicks on a my notification they are taken to "MyActivity".

How can I create a notification that does nothing?

Notification notification = new Notification(R.drawable.success, res.getString(R.string.messages_sent), System.currentTimeMillis());

        //Define the expanded message and intent
        Context context = getApplicationContext();
        CharSequence contentTitle = res.getString(R.string.messages_sent_ellipsis);


        Intent notificationIntent = new Intent(this, MyActivity.class);
        PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0 );
        notification.setLatestEventInfo(context, contentTitle, mStrSuccessMessage, contentIntent);
        notification.flags |= Notification.FLAG_AUTO_CANCEL;
        //Pass the notification to the notification manager
        mNotificationManager.notify(1, notification);

回答1:


Changing

Intent notificationIntent = new Intent(this, MyActivity.class);

to

Intent notificationIntent = new Intent();

would do the job. The gist is if you want nothing, give it a blank intent.



来源:https://stackoverflow.com/questions/7682182/how-to-write-a-notification-that-does-absolutly-nothing-when-clicked

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!