单击android时多个通知错误的内容

时间:2014-04-22 11:17:54

标签: android notifications

我使用下面的通知意图成功收到多个通知。但是当我点击任何通知时,它会打开每次收到的最后一个通知。谁能告诉我如何打开相应的通知。我的意思是每个通知都有自己的内容。

@Override
public void onReceive(Context context, Intent intent) {
    // TODO Auto-generated method stub

    /*
     *      Explicitly specify that GcmIntentService will handle the intent.
            ComponentName comp = new ComponentName(context.getPackageName(),
            GCMIntentService.class.getName());
            Start the service, keeping the device awake while it is launching.
            startWakefulService(context, (intent.setComponent(comp)));
            setResultCode(Activity.RESULT_OK);
     *
     */


    GCMNotificationUtility.notiMsg = intent.getStringExtra("msg");
    GCMNotificationUtility.notPubDate = chopJson(intent.getStringExtra("datepublished"));
    GCMNotificationUtility.notDescription =chopJson(intent.getStringExtra("description"));
    GCMNotificationUtility.notLink = chopJson(intent.getStringExtra("news_url"));
    GCMNotificationUtility.notCategory= chopJson(intent.getStringExtra("category"));



    int icon = R.drawable.ic_launcher; // icon from resources
    CharSequence tickerText =context.getResources().getString(R.string.app_name);//intent.getStringExtra("me"); // ticker-text

    long when = System.currentTimeMillis(); // notification time
    CharSequence xmsg =GCMNotificationUtility.notiMsg;


    NotificationManager notificationManager =
            (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    Notification notification = new Notification(icon, tickerText, when);


    System.out.println("nodescription "+GCMNotificationUtility.notDescription);

    ActusNewActivity.description=GCMNotificationUtility.notDescription;
    ActusNewActivity.pudDate=GCMNotificationUtility.notPubDate;
    ActusNewActivity.Link=GCMNotificationUtility.notLink;
    ActusNewActivity.title=GCMNotificationUtility.notiMsg;

    Intent notificationIntent = new Intent(context,ActusNewActivity.class);

    Calendar c = Calendar.getInstance(); 

    int unique_id = (int) c.getTimeInMillis();

    //set calendar.getTimeInMillis() as  unique ID.
    PendingIntent pendingIntent = PendingIntent.getActivity(context, unique_id , notificationIntent,  PendingIntent.FLAG_ONE_SHOT);

    notification.setLatestEventInfo(context,tickerText,xmsg, pendingIntent);
    notification.flags|=notification.FLAG_AUTO_CANCEL;
    notification.defaults |= Notification.DEFAULT_SOUND|Notification.DEFAULT_LIGHTS;
    notification.vibrate=new long[] {100L, 100L, 200L, 500L};
    notificationManager.notify(unique_id, notification);

    GCMNotificationUtility.notificationReceived=true;
    PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
    WakeLock wl = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP, "TAG");
    wl.acquire();       

}


public String chopJson(String y){

    String x = ":\"";
    int xval = y.indexOf(x)+2;

    String substring = y.substring(0, xval);
    String newElement =  y.replace(substring, "");
    int lastvalue = newElement.length()-2;
    String value = newElement.substring(0,lastvalue);

    return value;

}

1 个答案:

答案 0 :(得分:0)

而不是:

notificationManager.notify(unique_id, notification);

使用

notificationManager.notify((int) when , notification);

希望这有帮助。