2013-01-18 170 views
1

我有一個報警接收器從廣播中發出通知。完美運行。現在我添加了另一個通知,並且我喜歡單獨提供2個通知。開始第一次通知,稍後開始第二次通知,第二次通知取消第一次通知,所以我只看到第二次通知。我需要在通知欄上有2個通知。另外刪除「標誌取消」不運行。兩個獨立的通知

我的代碼(第二通知):

String ns2 = Context.NOTIFICATION_SERVICE; 
      NotificationManager mNotificationManager2 = (NotificationManager) context.getSystemService(ns2); 

      int icon = R.drawable.ic_launcherlight; 
      CharSequence tickerText = not1[x]; 
      long when = System.currentTimeMillis(); 

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

      CharSequence contentTitle = "Casual"; 
      CharSequence contentText = not1[x]; 

      Intent notificationIntent = new Intent("com.example.myapp", null, context, NotifyCasual.class); 
      notificationIntent.putExtra("notify", not1[x]); 
      PendingIntent contentIntent = PendingIntent.getActivity(context, 2, notificationIntent,PendingIntent.FLAG_CANCEL_CURRENT); 



      notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent); 


      SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); 

      String Sound = prefs.getString("sound", ""); 
notification.defaults |= Notification.DEFAULT_VIBRATE; 
      notification.defaults |= Notification.FLAG_AUTO_CANCEL; 
      notification.flags |= Notification.FLAG_AUTO_CANCEL | Notification.FLAG_SHOW_LIGHTS; 

      final int HELLO2 = 1; 

      mNotificationManager2.notify(HELLO2, notification); 
+1

你到每一個通知給不同的通知ID? – Rahil2952

+0

咦?姆姆姆......不......如何設置ID?謝謝 –

+0

我剛剛給你答案看到它,並接受它作爲答案。 – Rahil2952

回答

2

你要做這樣的。 當您創建通知時,請按照此操作。

對於第一個通知:

mNotificationManager.notify(1, notifyDetails); 

對於第二個通知:

mNotificationManager.notify(2, notifyDetails); 
+0

所以,我有2類:第一類與一些相同類型的通知。另一類與其他類型的通知。 –

+0

解決了!謝謝! –

+0

k很好.. – Rahil2952

0

讓你的每一條通知意圖通過使用setData.i使用 獨特當= Calendar.getInstance()getTimeInMillis()

嘗試如下。代碼

/*create intent for show notification details when user clicks notification*/ 
    Intent intent =new Intent(getApplicationContext(), NotificationDetailsActivity.class); 
    intent.putExtra(NOTIFICATION_DATA, notificationData); 

    /*create unique this intent from other intent using setData */ 
    intent.setData(Uri.parse("content://"+when)); 
    /*create new task for each notification with pending intent so we set Intent.FLAG_ACTIVITY_NEW_TASK */ 
    PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, intent, Intent.FLAG_ACTIVITY_NEW_TASK); 

    /*get the system service that manage notification NotificationManager*/ 
    NotificationManager notificationManager =(NotificationManager) getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE); 

    /*build the notification*/ 
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(
      getApplicationContext()) 
      .setWhen(when) 
      .setContentText(notificationContent) 
      .setContentTitle(notificationTitle) 
      .setSmallIcon(smalIcon) 
      .setAutoCancel(true) 
      .setTicker(notificationTitle) 
      .setLargeIcon(largeIcon) 
      .setDefaults(Notification.DEFAULT_LIGHTS| Notification.DEFAULT_VIBRATE| Notification.DEFAULT_SOUND) 
      .setContentIntent(pendingIntent); 

    /*Create notification with builder*/ 
    Notification notification=notificationBuilder.build(); 

    /*sending notification to system.Here we use unique id (when)for making different each notification 
    * if we use same id,then first notification replace by the last notification*/ 
    notificationManager.notify((int) when, notification); 
+0

欲瞭解更多信息,請訪問http://sanathnandasiri.blogspot.com/2012/12/android-notification-with.html – Sanath

1

爲了得到你有一個新的通知,每次每次都通過不同的ID進行的PendingIntent,

所以,你必須創建一個動態ID

int id = 0; // declare globally and save the id. 
PendingIntent contentIntent = PendingIntent.getActivity(context, id, 
         notificationIntent,PendingIntent.FLAG_CANCEL_CURRENT); 
id = id + 1;