【问题标题】:How to create Multiple statusbar Notifications in android如何在android中创建多个状态栏通知
【发布时间】:2011-06-03 07:23:42
【问题描述】:

我需要创建多个状态栏通知。当我拉下状态栏时,应将多个通知图标显示为列表。每个通知图标应显示单独的数据以显示在下一页上。我该怎么做?

我的代码:

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);
        }
    });

这里我做了一个通知,但是我需要创建多个通知,每个通知应该显示每个数据。

【问题讨论】:

标签: android android-notifications


【解决方案1】:

您需要为每个通知传递一个唯一的 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);
    }
}

MyActivityA - 在 onCreate() 中

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

【讨论】:

  • 如何显示两个通知的数据?
  • @rochdev 您的代码是正确的,但是如何在每次通知点击时获得不同的数据?我试过了。我会收到多个通知,但每次通知点击都会发送相同的意图数据。我需要不同的每个通知上的数据单击哪个服务器在有效负载包中发送它。我该怎么做?
  • @rochdev 这将创建新的通知,如果我想用最新消息和旧消息更新以前的消息
【解决方案2】:

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

如果您重复使用该 ID,则不会添加新 ID,而是会更新旧 ID。

这里是如何使用notifications的指南。

【讨论】:

  • 如何添加 ID 以便与数据库中的 _id 类似?每个新的 +1 ?
【解决方案3】:

您必须更改 通知 id,因为解决方案始终不是您必须使用 ramdom 数字概念

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

【讨论】:

    【解决方案4】:

    This example 展示了如何创建多个 Notification

    【讨论】:

      【解决方案5】:

      如果您想在每个通知上显示不同的数据。在待处理的Intent 中使用标志FLAG_UPDATE_CURRENT

      PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
      

      它会更新每个通知的数据,而无需每次都重新创建。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-08-23
        • 2014-09-24
        • 1970-01-01
        相关资源
        最近更新 更多