GCM - 多个通知 [英] GCM - multiple notifications

查看:115
本文介绍了GCM - 多个通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Google Cloud Messaging(仅限一种方式,即服务器到客户端)接收推送通知。
我收到了这些信息,起初一切都很好,但现在,过了一个多星期后,我收到了多个通知; 3个在一个设备上,另一个在7个。



我的理论是设备注册不同的id,因此得到多个通知。

但为什么会这样呢?我正在使用谷歌示例Android项目,调整它来挖掘,但看不到会导致这些多次注册。
是否因为注册ID已过期?



编辑:服务器上的所有registrationIds都不同。

EDIT2:我想我在活动的onDestroy()方法中调用了GCMRegistrar(context),但实际上并没有在我的服务器上取消注册注册ID。这可能是问题吗?如果注册ID是未注册的,那么这意味着什么?

解决方案

(GCM引擎可能会在它确定需要时更改注册ID,并且这可能会在任何时候发生而没有事先警告,这就是为什么您应该始终检查最后一个注册ID是否与当前注册ID匹配的原因),我有这个解决方案:


  1. 使用registerDevice(GCM_ID,DeviceID)之类的webservice函数
  2. 现在从DB中读取GCMIdColumn, DeviceIDColumn = DeviceID。如果此GCMIdColumn!= GCM_ID,则更新。

  3. 如果没有包含DeviceIDColumn = DeviceID的行,则插入(GCM_ID,DeviceID)。

DeviceID:从应用中获取的ID。
GCM_ID:由GCM返回的ID。



现在唯一必须确定的是您从应用生成的DeviceID应该是唯一的。您可以将设备配置的两个或更多属性组合为唯一。



生成设备ID

  public String getDeviceID(Context context){
TelephonyManager TelephonyMgr =(TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
String szImei = TelephonyMgr.getDeviceId(); //需要READ_PHONE_STATE
String m_szDevIDShort =35+ //我们使它看起来像一个有效的IMEI
Build.BOARD.length()%10+ Build.BRAND.length()%10 +
Build.CPU_ABI.length()%10 + Build.DEVICE.length()%10 +
Build.DISPLAY.length()%10 + Build.HOST.length()%10 +
Build.ID.length()%10 + Build.MANUFACTURER.length()%10 +
Build.MODEL.length()%10 + Build.PRODUCT.length()%10 +
Build .TAGS.length()%10 + Build.TYPE.length()%10 +
Build.USER.length()%10; // 13位
if(Utills.debug)
Log.i(getDeviceID,szImei +== XXX ==+ m_szDevIDShort);
return szImei +== XXX ==+ m_szDevIDShort;
}

希望这是您正在寻找的解决方案!


I am using Google Cloud Messaging (one way only, i.e. server to client) to receive Push-notifications. I am receiving these, and at first everything was fine, but now, after a bit over a week, I get multiple notifications; 3 on one device, and 7 on the other.

My theory is that the devices register with different ids, and thus get multiple notifications.

But why can that be? I am using Googles sample Android project, adapted it to mine, but can't see what would cause these multiple registrations. Is it because the registration ids have expired?

EDIT: All registrationIds on the sever are different however.

EDIT2: I think I called GCMRegistrar(context) in the onDestroy()-method of the activity, but didn't actually unregister the registration id on my server. Might that be the problem? What does it mean if the registration id is unregistered?

解决方案

After reflecting on your latest comment (The GCM engine might change the registration ID whenever it determines it need to, and this might happen at anytime without prior warning, that's why you should always check if the last registration ID matches the one that is current), I have this solution:

  1. Have webservice function like registerDevice(GCM_ID,DeviceID)
  2. Now from DB fetch the GCMIdColumn where DeviceIDColumn = DeviceID. If this GCMIdColumn != GCM_ID, then update. Else do nothing.
  3. If there is no row with having DeviceIDColumn = DeviceID, then insert (GCM_ID,DeviceID).

DeviceID: A ID picked up from the App. GCM_ID: Id returned by GCM.

Now the only thing you must be certain about is that DeviceID you generate from the app should be unique. You can combine 2 or more properties of the device's configuration to make it unique.

Generate Device ID

public String getDeviceID(Context context) {
        TelephonyManager TelephonyMgr = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
        String szImei = TelephonyMgr.getDeviceId(); // Requires READ_PHONE_STATE
        String m_szDevIDShort = "35" + //we make this look like a valid IMEI
                Build.BOARD.length()%10+ Build.BRAND.length()%10 +
                Build.CPU_ABI.length()%10 + Build.DEVICE.length()%10 +
                Build.DISPLAY.length()%10 + Build.HOST.length()%10 +
                Build.ID.length()%10 + Build.MANUFACTURER.length()%10 +
                Build.MODEL.length()%10 + Build.PRODUCT.length()%10 +
                Build.TAGS.length()%10 + Build.TYPE.length()%10 +
                Build.USER.length()%10 ; //13 digits
        if(Utills.debug)
            Log.i("getDeviceID",szImei+"==XXX=="+m_szDevIDShort);
        return szImei+"==XXX=="+m_szDevIDShort;
    }

Hope this is the solution you are looking for!

这篇关于GCM - 多个通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆