0

Google プッシュ通知の受信中に以下の問題に直面しています。以下の PHP からの通知を送信し、Android GCMBaseIntentService で一意の ID を受け取るためのタグ data.notificationID をもう 1 つ追加しています。

    $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

        }

    }

以下の行では、notificationID の有効期限が切れていますが、新しい ID を送信するたびに。

generateNotification(arg0, arg1.getStringExtra("メッセージ") , arg1.getStringExtra("通知ID") );

もしあればバグを見つけるのを手伝ってください。

4

1 に答える 1

9

これは一般的な問題です。保留中のインテントが更新されていません。次のように変更してください。

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