设备上的Lollipop更新后,Android GCM无法工作 [英] Android GCM not working after Lollipop update on device

查看:105
本文介绍了设备上的Lollipop更新后,Android GCM无法工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我针对Android指南中所述的推送通知实施了GCM( https://开发者.android.com / google / gcm / client.html )在我的一个应用程序。应用程序和通知在Kitkat和棒棒糖上运行良好。

但最后,我成为用户将电话升级为棒棒糖的邮件。这样通知就不会再显示了。目前唯一的解决方案是删除应用程序,并从appstore重新安装。



有人遇到过类似的问题,如果是的话,您是否找到解决方案来解决它?

解决方案

这是一个GCM ID问题。尝试使用Thread.sleep并重试多次,直到收到GCM ID。

  int noOfAttemptsAllowed = 5; //允许的重试次数
int noOfAttempts = 0; //完成的尝试次数
bool stopFetching = false; //标志表示是否必须重试
String regId =;


while(!stopFetching)
{
noOfAttempts ++;
GCMRegistrar.register(getApplicationContext(),XXXX_SOME_KEY_XXXX);
尝试
{
//在此处留出一些时间以便注册成为
// //在进入下一行之前注册
Thread.sleep(2000); //根据试用设置这个时间。
} catch(InterruptedException e){
e.printStackTrace();
}
尝试
{
//获取注册ID
regId = GCMRegistrar.getRegistrationId(LoginActivity.this);
} catch(Exception e){}


if(!regId.isEmpty()|| noOfAttempts> noOfAttemptsAllowed)
{
//如果获得的注册ID或尝试次数超过,请停止获取
stopFetching = true;

if(!regId.isEmpty())
{
//如果注册ID已获取,则保存到共享首选项
saveRegIDToSharedPreferences();
}


}

线程。睡眠和noOfAttemptsAllowed可以根据您的设计和其他参数进行调整。我们有一个7000的睡眠时间,以便第一次尝试登记的概率更高。但是,如果失败,下一次尝试将耗用7000毫秒。这可能会导致用户认为你的应用程序很慢。所以,用这两个值来智能地玩耍。


I implemented GCM for push notifications like stated in the Android Guide (https://developer.android.com/google/gcm/client.html) in one of my apps. The app and notifications are working fine on Kitkat and Lollipop.

But lastly I became some mails from users that upgraded their phones from to Lollipop. With that the notifications will not be displayed anymore. Only solution so far is to remove the app and reinstall it from the appstore.

Did someone face a similar problem and if so, did you find a solution to fix it?

解决方案

This is a GCM ID issue. Try using Thread.sleep and retry for a number of times, till the GCM ID is recieved.

int noOfAttemptsAllowed = 5;   // Number of Retries allowed
  int noOfAttempts = 0;          // Number of tries done
  bool stopFetching = false;     // Flag to denote if it has to be retried or not
  String regId = "";             


  while (!stopFetching) 
  {
       noOfAttempts ++;
       GCMRegistrar.register(getApplicationContext(), "XXXX_SOME_KEY_XXXX");
       try 
       {
          // Leave some time here for the register to be 
          // registered before going to the next line
          Thread.sleep(2000);   // Set this timing based on trial.
   } catch (InterruptedException e) {
    e.printStackTrace();
   }
       try
       {
            // Get the registration ID
            regId = GCMRegistrar.getRegistrationId(LoginActivity.this);
       } catch (Exception e) {}


       if (!regId.isEmpty() || noOfAttempts > noOfAttemptsAllowed)
       {
            // If registration ID obtained or No Of tries exceeded, stop fetching
            stopFetching = true;
        }
        if (!regId.isEmpty())
        {
            // If registration ID Obtained, save to shared preferences
            saveRegIDToSharedPreferences(); 
        }


   }

The Thread.sleep and noOfAttemptsAllowed can be played around with based on your design and other parameters. We had a sleep time of 7000 so that probability of getting registered at first attempt is higher. However, if it fails, the next attempt would consume another 7000ms. This might cause users to think your app is slow. So, play around intelligently with those two values.

这篇关于设备上的Lollipop更新后,Android GCM无法工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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