lundi 20 avril 2015

When one of the Multiple push notification present in the tray is touched others vanish in android

Vote count: 0

In statusbar when more than two notification display.when I tap on first and second notification its work fine for me.when I tap on second notification all remaining notification vanish it out. "GCMIntentService.java" file

   package com.plugin.gcm;

 import org.json.JSONException;
 import org.json.JSONObject;
 import android.annotation.SuppressLint;
 import android.app.Notification;
 import android.app.NotificationManager;
 import android.app.PendingIntent;
 import android.content.Context;
 import android.content.Intent;
 import android.os.Bundle;
 import android.support.v4.app.NotificationCompat;
 import android.util.Log;
 import java.util.Random;

import com.google.android.gcm.GCMBaseIntentService;

 @SuppressLint("NewApi")
public class GCMIntentService extends GCMBaseIntentService {

private static final String TAG = "GCMIntentService";

public GCMIntentService() {
    super("GCMIntentService");
}

@Override
public void onRegistered(Context context, String regId) {

    Log.v(TAG, "onRegistered: "+ regId);

    JSONObject json;

    try
    {
        json = new JSONObject().put("event", "registered");
        json.put("regid", regId);

        Log.v(TAG, "onRegistered: " + json.toString());

        // Send this JSON data to the JavaScript application above EVENT should be set to the msg type
        // In this case this is the registration ID
        PushPlugin.sendJavascript( json );

    }
    catch( JSONException e)
    {
        // No message to the user is sent, JSON failed
        Log.e(TAG, "onRegistered: JSON exception");
    }
}

@Override
public void onUnregistered(Context context, String regId) {
    Log.d(TAG, "onUnregistered - regId: " + regId);
}

@Override
protected void onMessage(Context context, Intent intent) {
    Log.d(TAG, "onMessage - context: " + context);

    // Extract the payload from the message
    Bundle extras = intent.getExtras();
    if (extras != null)
    {
        // if we are in the foreground, just surface the payload, else post it to the statusbar
        if (PushPlugin.isInForeground()) {
            extras.putBoolean("foreground", true);
            PushPlugin.sendExtras(extras);
        }
        else {
            extras.putBoolean("foreground", false);

            // Send a notification if there is a message
            if (extras.getString("message") != null && extras.getString("message").length() != 0) {
                createNotification(context, extras);
            }
        }
    }
}

public static final int NOTIFICATION_ID = 237;
public static int MY_NOTIFICATION_ID = 237;

public void createNotification(Context context, Bundle extras)
{
    int notId = 0;
     Random rnd = new Random();  
    try {
    //  notId = Integer.parseInt(extras.getString("notId",Integer.toString(rnd.nextInt(1000))));
        notId = rnd.nextInt(9999 - 1000) + 1000;
    }
    catch(NumberFormatException e) {
        Log.e(TAG, "Number format exception - Error parsing Notification ID: " + e.getMessage());
    }
    catch(Exception e) {
        Log.e(TAG, "Number format exception - Error parsing Notification ID" + e.getMessage());
    }


    NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    String appName = getAppName(this);

    Intent notificationIntent = new Intent(this, PushHandlerActivity.class);
    notificationIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
    notificationIntent.putExtra("pushBundle", extras);

    PendingIntent contentIntent = PendingIntent.getActivity(this, notId, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);


    int defaults = Notification.DEFAULT_ALL;

    if (extras.getString("defaults") != null) {
        try {
            defaults = Integer.parseInt(extras.getString("defaults"));
        } catch (NumberFormatException e) {}
    }

    NotificationCompat.Builder mBuilder =
        new NotificationCompat.Builder(context)
            .setDefaults(defaults)
            .setSmallIcon(context.getApplicationInfo().icon)
            .setWhen(System.currentTimeMillis())
            .setContentTitle(extras.getString("title"))
            .setTicker(extras.getString("title"))
            .setContentIntent(contentIntent)
            .setAutoCancel(true);

    String message = extras.getString("message");
    if (message != null) {
        mBuilder.setContentText(message);
    } else {
        mBuilder.setContentText("<missing message content>");
    }

    String msgcnt = extras.getString("msgcnt");
    if (msgcnt != null) {
        mBuilder.setNumber(Integer.parseInt(msgcnt));
    }

    MY_NOTIFICATION_ID++;
      mNotificationManager.notify((String) appName, MY_NOTIFICATION_ID, mBuilder.build());


}


private static String getAppName(Context context)
{
    CharSequence appName = 
            context
                .getPackageManager()
                .getApplicationLabel(context.getApplicationInfo());

    return (String)appName;
}

@Override
public void onError(Context context, String errorId) {
    Log.e(TAG, "onError - errorId: " + errorId);
}

public static void cancelNotification(Context context)
{
    NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    //mNotificationManager.cancel((String)getAppName(context), MY_NOTIFICATION_ID);
    mNotificationManager.cancelAll();
    MY_NOTIFICATION_ID = NOTIFICATION_ID;
  }

    }
  Referlink:
  http://ift.tt/1JZcQ7g

 Please Help me out

asked 3 mins ago



When one of the Multiple push notification present in the tray is touched others vanish in android

Aucun commentaire:

Enregistrer un commentaire