android通知问题

sar*_*ath 0 notifications android

我正在开发一个Android应用程序.该应用程序就像本机短信应用程序.我在通知部分遇到问题.在本机应用程序中,他们以下列方式处理通知.

1)如果来自特定号码的消息,则点击通知将导致相应联系人的聊天页面,并且通知将被清除.

2)如果来自不同号码的消息,则点击通知将导致主页和通知不会清除.

我完成了第一部分,我不知道第二部分.有没有办法让通知部分没有明确并调用意图.

Sun*_*nic 6

你必须添加一个标志:

 notification.flags |= Notification.FLAG_ONGOING_EVENT;
Run Code Online (Sandbox Code Playgroud)

这是一个完整的例子:

 private static final int NOTIFICATION_EX = 0;
 private NotificationManager notificationManager;
Run Code Online (Sandbox Code Playgroud)

...

in onCreate:

    notificationManager = (NotificationManager) 
    getSystemService(Context.NOTIFICATION_SERVICE);



int icon = R.drawable.youricon;
    CharSequence tickerText = "Sticky notification";
    long when = System.currentTimeMillis();

    Notification notification = new Notification(icon, tickerText, when);

    Context context = getApplicationContext();
    CharSequence contentTitle = "Sticky notification";
    CharSequence contentText = "...click here and it wont go away...";
    Intent notificationIntent = new Intent(this, mainmenu.class).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
            | Intent.FLAG_ACTIVITY_SINGLE_TOP);
    PendingIntent contentIntent = PendingIntent.getActivity(this, 
        0, notificationIntent, 0);

    notification.setLatestEventInfo(context, contentTitle, 
        contentText, contentIntent);
    notification.flags |= Notification.FLAG_ONGOING_EVENT;

    notificationManager.notify(NOTIFICATION_EX, notification);
Run Code Online (Sandbox Code Playgroud)

您可以根据自己的喜好调整意图.