2016-03-17 5 views
0

Я использовал уведомления GCM Push в своем приложении. Все отлично.как обращаться с различными уведомлениями об GCM.

Когда я получаю уведомление, и когда я нажимаю уведомление, я могу переместить другую активность. Я прохожу ссылку url к другому activity.but, как обрабатывать клики по нескольким уведомлениям.

Здесь говорят, что когда я получаю 5 уведомлений и нажимаю на любое уведомление, я перехожу к другой деятельности, но ссылка, которую я передаю, является первым URL-адресом уведомления. Я использую различные ID уведомления

NotificationManager notificationManager = 

(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE); 

notificationManager.notify(x 
        /*ID of notification */ 
       , notificationBuilder.build()); 
+1

Как этот вопрос отличается от http://stackoverflow.com/questions/36055555/how-to-handle-gcm-notifications-when-different-gcm-notifications-receive? – Michael

+0

Можете ли вы сказать мне решение – Sanjeev

+1

Нет. И вы не должны публиковать один и тот же вопрос несколько раз. См. Http://stackoverflow.com/help/no-one-answers – Michael

ответ

0

использовать PendingIntent.FLAG_UPDATE_CURRENT при создании PendingIntent.

Также перед тем, как передать намерение PendingIntent, каждое действие будет действовать каждый раз. например: -

intent.setAction(Long.toString(System.currentTimeMillis())); 
+0

, где я должен это делать в ожидании намерения? PendingIntent pendingIntent = PendingIntent.getActivity (это 0 /* Код запроса */ , намерение, PendingIntent.FLAG_ONE_SHOT); – Sanjeev

+0

yes in PendingIntent – maveroid

+0

Позвольте мне попробовать, пожалуйста, помогите, я застрял с утра – Sanjeev

0

Смотрите ниже кода для обработки несколько уведомлений с различной обработкой данных:

private void sendNotification(Bundle extras) 
{ 
    String detail = extras.getString("detail"); 
    String title = extras.getString("title"); 
    if((title==null || title.trim().length()==0) && (detail==null || detail.trim().length()==0)) 
    { 
     //title=getString(R.string.app_name); 
     return; 
    } 
    NOTIFICATION_ID = (int) (System.currentTimeMillis()/1000L); 

    Intent notificationIntent = new Intent(this, ContainerActivity.class); 
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP); 
    String data = extras.getString("data"); 
    notificationIntent.putExtra("data",data); 
    PendingIntent pendingIntent = PendingIntent.getActivity(this, NOTIFICATION_ID, notificationIntent, PendingIntent.FLAG_CANCEL_CURRENT); 

    NotificationCompat.BigTextStyle bigTextStyle = new NotificationCompat.BigTextStyle(); 
    bigTextStyle = bigTextStyle.bigText(detail); 

    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this) 
    .setContentTitle(title) 
    .setContentText(detail) 
    .setContentIntent(pendingIntent) 
    .setSmallIcon(R.drawable.app_logo) 
    .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.app_logo)) 
    .setWhen(System.currentTimeMillis()) 
    .setAutoCancel(true) 
    .setVibrate(new long[] { 0, 100, 200, 300 }) 
    .setPriority(NotificationCompat.PRIORITY_MAX) 
    .setStyle(bigTextStyle); 

    mBuilder.setDeleteIntent(getDeleteIntent(NOTIFICATION_ID)); 

    Set<String> pnIdsSet = ((MyAccountApplication)getApplication()).getPrefs().getStringSet("PUSH_IDS", null); 
    if(pnIdsSet==null) 
    { 
     pnIdsSet=new HashSet<String>(); 
    } 
    pnIdsSet.add(""+NOTIFICATION_ID); 
    ((MyAccountApplication)getApplication()).getPrefs().edit().putStringSet("PUSH_IDS", pnIdsSet).commit(); 

    mBuilder.setContentIntent(pendingIntent); 
    Notification n = mBuilder.build(); 

    n.flags |= Notification.FLAG_SHOW_LIGHTS; 
    n.flags |= Notification.FLAG_AUTO_CANCEL; 
    n.defaults = Notification.DEFAULT_ALL;  
    n.when = System.currentTimeMillis(); 

    mNotificationManager = (NotificationManager)this.getSystemService(Context.NOTIFICATION_SERVICE); 
    mNotificationManager.notify(NOTIFICATION_ID, n); 
} 
private PendingIntent getDeleteIntent(int pnId) 
{ 
    Intent intent = new Intent(this, NotificationBroadcastReceiver.class); 
    intent.setAction("notification_cancelled"); 
    intent.putExtra("PN_ID", ""+pnId); 
    return PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT); 
} 

в коде выше я обработка уведомлений удалить намерение также увидеть каждое уведомление имеет различные уведомления идентификатора с PendingIntent. Флаг FLAG_CANCEL_CURRENT.

Смежные вопросы