通过谷歌推送通知接收来自额外密钥的旧值

Posted

技术标签:

【中文标题】通过谷歌推送通知接收来自额外密钥的旧值【英文标题】:Receiving old value from extra key by google push notification 【发布时间】:2012-11-27 07:47:10 【问题描述】:

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

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

通知正确发送,但 notificationID 值较旧,这是我先推送的。它不是链接。但是我发送的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("message") , arg1.getStringExtra("notificationID") );

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

【问题讨论】:

更多的 php 问题,因为它的服务器部分似乎是错误的...... 【参考方案1】:

这是一个常见问题,未更新的待处理意图, 改成这样:

PendingIntent intent = PendingIntent.getActivity(context, 0,
                    notificationIntent,  PendingIntent.FLAG_UPDATE_CURRENT);

【讨论】:

谢谢@Anis!很有帮助,我一直想知道为什么我的 GCM 意图数据在过去 2 天没有更新! @gauravsanu 请接受答案:)

以上是关于通过谷歌推送通知接收来自额外密钥的旧值的主要内容,如果未能解决你的问题,请参考以下文章

谷歌推送通知未到达应用程序

无法从 android 上的解析推送通知接收额外数据

在android中没有收到推送通知

来自 UrbanAirship 的推送通知不适用于 android 中的实时服务器(即使用生产密钥)

仅接收来自 GCM 的最后一个后台静默推送通知

无法接收来自 parse.com 的推送通知