如何打开第一条通知消息

时间:2016-03-16 09:12:20

标签: android android-notifications

我有一个GcmIntentService类, 这是代码:

    public class GcmIntentService extends IntentService {
    public static  int NOTIFICATION_ID = 1;
    private NotificationManager mNotificationManager;
    NotificationCompat.Builder builder;
    int numMessages ;
    Boolean isSaveDetails ;
    Boolean isRememberMe ;
    public static final String PREFS_NAME = "MyPrefsFile";
    public GcmIntentService() {
        super("GcmIntentService");
    }
    public static final String TAG = "GCM Demo";

    @SuppressWarnings("deprecation")
    @Override
    protected void onHandleIntent(Intent intent) {
        Bundle extras = intent.getExtras();
       GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);
        //The getMessageType() intent parameter must be the intent you received
         //in your BroadcastReceiver.
        String messageType = gcm.getMessageType(intent);

        if (!extras.isEmpty()) {  // has effect of unparcelling Bundle
            /*
             * Filter messages based on message type. Since it is likely that GCM will be
             * extended in the future with new message types, just ignore any message types you're
             * not interested in, or that you don't recognize.
             */
           if (GoogleCloudMessaging.MESSAGE_TYPE_SEND_ERROR.equals(messageType)) {
               // sendNotification(extras.toString());
           } else if (GoogleCloudMessaging.MESSAGE_TYPE_DELETED.equals(messageType)) {
                //sendNotification("Deleted messages on server: " + extras.toString());
           // If it's a regular GCM message, do some work.
           } else if (GoogleCloudMessaging.MESSAGE_TYPE_MESSAGE.equals(messageType)) {
                // This loop represents the service doing some work.
                for (int i = 0; i < 1; i++) {
                    Log.i(TAG, "Working... " + (i + 1)
                            + "/5 @ " + SystemClock.elapsedRealtime());
                    try {
                        Thread.sleep(5000);
                    } catch (InterruptedException e) {
                    }
                }
                Log.i(TAG, "Completed work @ " + SystemClock.elapsedRealtime());
                // Post notification of received message.
                sendNotification( extras);
                Log.i(TAG, "Received: " + extras.toString());
            }
        }
        // Release the wake lock provided by the WakefulBroadcastReceiver.
       GcmBroadcastReceiver.completeWakefulIntent(intent);
  }

    // Put the message into a notification and post it.
    // This is just one simple example of what you might choose to do with
    // a GCM message.
    private void sendNotification(Bundle extras) {
        String msg = URLDecoder.decode(extras.getString("message"));
        String msg1="";
        String sender = extras.getString("sender");
        mNotificationManager = (NotificationManager)
                this.getSystemService(Context.NOTIFICATION_SERVICE);
        String type= extras.getString("messageType");
        PendingIntent contentIntent ;
        SharedPreferences pref = getSharedPreferences(PREFS_NAME,MODE_PRIVATE); 
        isRememberMe= pref.getBoolean("rememberMe", false); 

        if(type.equals("add_contacting")||type.equals("update_contacting_inspector")||type.equals("update_contacting_admin"))
        {
            Intent demoactivity;
             msg1=msg;
             String contactingId= extras.getString("contactingId");
             boolean foregroud=true;
            try {
                foregroud = new ForegroundCheckTask().execute(getApplicationContext()).get();
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (ExecutionException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
             if(!isRememberMe&&foregroud==false)//login page will open only if the app isn't on  Foreground
                demoactivity = new Intent(this,LoginActivity.class);
             else
                demoactivity = new Intent(this,Show_Contacting_page.class);
            demoactivity.putExtra("calling-activity", moveBetweenActivities.GCM_INTENT_SERVICES);
            demoactivity.putExtra("message",msg);
            demoactivity.putExtra("sent_from",sender); 
            demoactivity.putExtra("contactingId",contactingId); 
            contentIntent = PendingIntent.getActivity(this, 0,demoactivity, PendingIntent.FLAG_UPDATE_CURRENT);
       }

        else
        {
            msg1=msg;
            Intent demoactivity = new Intent(this,Show_Contacting_page.class);
            demoactivity.putExtra("message",msg);
            demoactivity.putExtra("sent_from",sender);  
            contentIntent = PendingIntent.getActivity(this, 0,demoactivity, PendingIntent.FLAG_UPDATE_CURRENT);

        }
        NotificationCompat.Builder mBuilder =
                new NotificationCompat.Builder(this)
        .setSmallIcon(R.drawable.rh_logo)
        .setContentTitle(getString(R.string.notification_message_title))
        .setStyle(new NotificationCompat.BigTextStyle()
        .bigText(msg1))
        .setWhen(System.currentTimeMillis());
        numMessages = 0;
         mBuilder.setContentText(msg1)
          .setAutoCancel(true);
        mBuilder.setContentIntent(contentIntent);
        mNotificationManager.notify(++NOTIFICATION_ID, mBuilder.build());



        Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        Ringtone r = RingtoneManager.getRingtone(getApplicationContext(), notification);
        r.play();

    }

我的问题是:如果应用获得两个或更多通知,并且它们都在actionBar上显示, 当用户点击actionBar中的第一条通知消息时,将打开最后一条通知,

(我认为第一封通知消息未保存。)

有人可以向我解释我的代码有什么问题吗?代码中要改变什么?

0 个答案:

没有答案