0

我在接收谷歌推送通知时面临以下问题。我正在从 PHP 提交以下通知,并添加一个标签 data.notificationID 以在 android GCMBaseIntentService 中接收唯一 ID。

    $data = array(
            'registration_id' => $deviceToken,
            'collapse_key' => $collapseKey,
            'data.message' => $messageText ,
            'data.notificationID' => $yourKey 
            );

通知正确发送,但我首先推送的通知 ID 值较旧。它不是链接。但是我已经发送了新的data.message消息字符串。

php中的推送通知代码为:

        <?php       
            $Key    =   "0000000";
            $deviceToken    =   "sdfsjdfljsd";
            $collapseKey    =   1;
            $messageText    =   "Hi welcome";
            //$yourKey      =   100;
            $yourKey        =   101;

        $headers = array('Authorization:key=' . $Key);
        $data = array(
            'registration_id' => $deviceToken,
            'collapse_key' => $collapseKey,
            'data.message' => $messageText ,
            'data.notificationID' => $yourKey 
            );

        $ch = curl_init();

        curl_setopt($ch, CURLOPT_URL, "https://android.googleapis.com/gcm/send");
        if ($headers)
            curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

        $response = curl_exec($ch);
        $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        if (curl_errno($ch)) {
            return false;
        }
        if ($httpCode != 200) {
            return false;
        }
        curl_close($ch);
    ?>

在 Android GCMIntentService 实现中:

        import android.app.IntentService;
    import android.app.Notification;
    import android.app.NotificationManager;
    import android.app.PendingIntent;
    import android.content.Context;
    import android.content.Intent;
    import android.graphics.Color;
    import android.os.Bundle;
    import android.util.Log;
    import android.widget.Toast;

    import com.google.android.gcm.GCMBaseIntentService;
        public class GCMIntentService extends GCMBaseIntentService {

        @SuppressWarnings("hiding")
        private static final String TAG = "GCMIntentService";

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

        /**
         * Issues a notification to inform the user that server has sent a message.
         */
        private static void generateNotification(Context context, String message , String notificationID ) {
            long when = System.currentTimeMillis();
            NotificationManager notificationManager = (NotificationManager) context
                    .getSystemService(Context.NOTIFICATION_SERVICE);
            Notification notification = new Notification(R.drawable.ic_launcher,
                    message, when);

            String title = context.getString(R.string.app_name);
            Intent notificationIntent = new Intent(context, ViewNotification.class);
            //notificationIntent.removeExtra("notificationID");
            notificationIntent.putExtra("notificationID", notificationID);
            // set intent so it does not start a new activity
            notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
                    | Intent.FLAG_ACTIVITY_SINGLE_TOP);
            PendingIntent intent = PendingIntent.getActivity(context, 0,
                    notificationIntent, 0);
            notification.setLatestEventInfo(context, title, message, intent);
            notification.flags = Notification.FLAG_SHOW_LIGHTS | Notification.FLAG_AUTO_CANCEL;
            notification.ledARGB = Color.BLUE;
            notification.ledOnMS = 1000;
            notification.ledOffMS = 300;


            notificationManager.notify( 1 , notification);
        }

        @Override
        protected void onError(Context arg0, String arg1) {
            // TODO Auto-generated method stub

        }

        @Override
        protected void onMessage(Context arg0, Intent arg1) {

            Log.d("GCM", "RECIEVED A MESSAGE");

            generateNotification(arg0, arg1.getStringExtra("message") , arg1.getStringExtra("notificationID") );

            //generateNotification(arg0, arg1.getStringExtra("key1"));
        }

        @Override
        protected void onRegistered(Context arg0, String arg1) {
            // TODO Auto-generated method stub


        }

        @Override
        protected void onUnregistered(Context arg0, String arg1) {
            // TODO Auto-generated method stub

        }

    }

在下面的行中,我的通知 ID 越来越旧,但每次我提交新 ID 时。

generateNotification(arg0, arg1.getStringExtra("message") , arg1.getStringExtra("notificationID") );

如果有,请帮助我找出错误。

4

1 回答 1

9

这是一个常见的问题,挂起的意图没有被更新,只需像这样更改:

PendingIntent intent = PendingIntent.getActivity(context, 0,
                    notificationIntent,  PendingIntent.FLAG_UPDATE_CURRENT);
于 2012-11-27T08:06:36.267 回答