如何在Android中创建多个状态栏通知

19

我需要创建多个状态栏通知。当我下拉状态栏时,应该显示多个通知图标作为列表。每个通知图标应该显示要在下一页上显示的不同数据。我该如何做到这一点?

我的代码:

public class SimpleNotification extends Activity {

private NotificationManager mNotificationManager;
private int SIMPLE_NOTFICATION_ID;

String str="Hai";
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    mNotificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
    final Notification notifyDetails = new Notification(R.drawable.android,"New Alert, Click Me!",System.currentTimeMillis());


    Button start = (Button)findViewById(R.id.notifyButton);
    Button cancel = (Button)findViewById(R.id.cancelButton);



        start.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {


            Context context = getApplicationContext();
            CharSequence contentTitle = "Notification Details...";
            CharSequence contentText = "Browse Android Official Site by clicking me";
            Intent notifyIntent = new Intent(SimpleNotification.this,
                    sub.class);

            Bundle bundle = new Bundle();
            bundle.putString("welcome",str);
            notifyIntent.putExtras(bundle);

            PendingIntent intent = 
                PendingIntent.getActivity(SimpleNotification.this, 0, 
                notifyIntent, android.content.Intent.FLAG_ACTIVITY_NEW_TASK);

            notifyDetails.setLatestEventInfo(context, contentTitle, contentText, intent);
            mNotificationManager.notify(SIMPLE_NOTFICATION_ID, notifyDetails);
        }
    });

我已经为一个通知做了,但我需要创建多个通知,每个通知都应显示每个数据。


可能是Multiple notifications to the same activity的重复问题。 - Balu SKT
5个回答

30
您需要为每个通知传递一个唯一的ID。一旦您点击了通知,您将使用该ID将其移除。
public class SimpleNotification extends Activity {

    private NotificationManager mNotificationManager;
    private int SIMPLE_NOTFICATION_ID_A = 0;
    private int SIMPLE_NOTFICATION_ID_B = 1;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

        Button start = (Button) findViewById(R.id.start_button);        

        start.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                // display A
                displayNotification("Extra for A", "This is A", "Some text for activity A", MyActivityA.class, SIMPLE_NOTFICATION_ID_A);
                // display B
                displayNotification("Extra for B", "This is B", "Some text for activity B", MyActivityB.class, SIMPLE_NOTFICATION_ID_B);
            }
        });
    }

    private void displayNotification(String extra, String contentTitle, String contentText, Class<?> cls, int id) {     
        Notification notifyDetails = new Notification(R.drawable.icon, "New Alert!", System.currentTimeMillis());
        Intent intent = new Intent(this, cls);
        intent.putExtra("extra", extra);
        PendingIntent contentIntent = PendingIntent.getActivity(getApplicationContext(), id, intent, PendingIntent.FLAG_ONE_SHOT);
        notifyDetails.setLatestEventInfo(getApplicationContext(), contentTitle, contentText, contentIntent);
        mNotificationManager.notify(id, notifyDetails);
    }
}

在 onCreate() 中的 MyActivityA

...
mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
mNotificationManager.cancel(SIMPLE_NOTFICATION_ID_A);
...

我该如何显示两个通知的数据? - sanjay
@rochdev,你的代码没问题,但我该如何在每次通知点击时获取不同的数据?我已经尝试过了。我会收到多个通知,但每次通知点击时都会发送相同的意图数据。我需要根据服务器在负载包中发送的内容,在每次通知点击时获取不同的数据。我该怎么做呢? - Nitish Patel
@rochdev 这将创建新的通知,如果我想使用最新消息更新先前的通知呢? - ingsaurabh

7

只需在mNotificationManager.notify(ID, notifyDetails);上使用不同的ID即可。

如果您重复使用ID,它将不会添加新的ID,而是更新旧的ID。

这里有一个关于如何使用通知的指南。


我该如何添加一个ID,使其像数据库中的_id一样?每次新建一个+1? - baTimá

2

你需要更改通知 ID,因为它一直不起作用。解决方案是使用随机数的概念。

Random random = new Random();
int randomNumber = random.nextInt(9999 - 1000) + 1000;
notificationManager.notify(randomNumber, notification);

1

0
如果您想在每个通知中显示不同的数据,请在待定的Intent中使用标志FLAG_UPDATE_CURRENT
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

每次通知时更新数据,无需每次重新创建。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接