启动服务时发生异常:com.google.android.c2dm.intent.REGISTER

问题描述:

我正在使用Urban Airship进行推送通知。该应用程序编译,但我没有收到推送通知,我看到这个在Android工作室日志:启动服务时发生异常:com.google.android.c2dm.intent.REGISTER

`D/Example - UALib: Starting GCM 
V/Example - UALib: Version code changed to 10. GCM re-registration required. 
D/Example - UALib: GCMRegistrar startService 
E/Example - UALib: An exception occurred when starting service: com.google.android.c2dm.intent.REGISTER 
         java.lang.IllegalArgumentException: Service Intent must be explicit: Intent { act=com.google.android.c2dm.intent.REGISTER (has extras) } 
          at android.app.ContextImpl.validateServiceIntent(ContextImpl.java:1813) 
          at android.app.ContextImpl.startServiceCommon(ContextImpl.java:1842) 
          at android.app.ContextImpl.startService(ContextImpl.java:1826) 
          at android.content.ContextWrapper.startService(ContextWrapper.java:516) 
          at com.urbanairship.push.GCMRegistrar.startService(GCMRegistrar.java:401) 
          at com.urbanairship.push.GCMRegistrar.register(GCMRegistrar.java:126) 
          at com.urbanairship.push.PushService.startPushService(PushService.java:306) 
          at com.urbanairship.push.PushService.onHandleIntent(PushService.java:136) 
          at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:65) 
          at android.os.Handler.dispatchMessage(Handler.java:102) 
          at android.os.Looper.loop(Looper.java:135) 
          at android.os.HandlerThread.run(HandlerThread.java:61)` 

在我的AndroidManifest.xml文件,我有这样的:

`<receiver android:name="com.urbanairship.push.GCMPushReceiver" android:permission="com.google.android.c2dm.permission.SEND"> 
    <intent-filter> 
     <action android:name="com.google.android.c2dm.intent.RECEIVE" /> 
     <action android:name="com.google.android.c2dm.intent.REGISTRATION" /> 
     <!-- MODIFICATION REQUIRED - Use your package name as the category --> 
     <category android:name="com.example" /> 
    </intent-filter> 
    <!-- REQUIRED for detecting when the application is upgraded so it can request a new GCM ID --> 
    <intent-filter> 
     <action android:name="android.intent.action.PACKAGE_REPLACED" /> 
     <data android:scheme="com.example"/> 
    </intent-filter> 
</receiver> 
<service android:name="com.urbanairship.push.PushService" android:label="Push Notification Service"/>` 

在我build.grandle文件,我为Android 6.0使用API​​ target 23的“targetSdkVersion 23”;如果我没有记错的话,API Level 16的“minSdkVersion 16”适用于Android 4.1.x。

我的城市飞艇配置是正确的,这得到了Urban Airship支持团队的确认。此问题与我的应用向GCM注册推送服务有关。这不是通过Urban Airship发生的,而是发生在应用程序和GCM本身之间的。一旦我的应用程序从GCM收到了有效的注册ID,该ID就会传递给Urban Airship。

我知道GCM已被弃用,并在https://developers.google.com/cloud-messaging/他们说:“Firebase云消息传递(FCM)是GCM的新版本”。我的问题是,我仍然可以使用API​​ Level 23的GCM,还是肯定必须从GCM升级到FCM才能使推送通知正常工作?我甚至试过,但我一直没能注册我的GCM应用的推送服务:

` 
Context context = getApplicationContext(); 
NotificationManager notificationManager = (NotificationManager) context 
    .getSystemService(Context.NOTIFICATION_SERVICE); 
GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this); 
try { 
    gcm.register(); 
} catch (IOException e) { 
    e.printStackTrace(); 
}` 
+0

在Android Studio中的日志中,在我看到“启动服务时发生异常:com.google.android.c2dm.intent.REGISTER”错误之前,我看到:V/GoogleSignatureVerifier:com.google.android。 gms签名无效。发现:MIIEQzCCAyugA ............. V/GoogleSignatureVerifier:com.google.android.gms签名无效。找到:MIIEQzCCAyugA ..................我怀疑这个“签名无效”消息可能与启动服务时发生的异常有关:com.google.android.c2dm .intent.REGISTER。 –

+0

此代码无效: String SENDER_ID = [My Google API Project Number]; 上下文context = getApplicationContext(); NotificationManager notificationManager =(NotificationManager)上下文 \t .getSystemService(Context.NOTIFICATION_SERVICE); GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(context); 尝试{ \t gcm.register(SENDER_ID); catch(IOException e){ \t e.printStackTrace(); } –

+0

尝试在上述注释中使用“gcm.register(SENDER_ID)”代码后出现错误:W/System.err:java.io.IOException:MAIN_THREAD W/System.err:at com.google。 android.gms.gcm.GoogleCloudMessaging.zzF(未知来源) W/System.err:com.google.android.gms.gcm.GoogleCloudMessaging.register(未知来源) W/System.err:at com.couponclub。 FirstActivity.onCreate(FirstActivity.java:83) W/System.err:at android.app.Activity.performCreate(Activity。java:6056) W/System.err:at android.app.Instrumentation.callActivityOnCreate(Instrumentation。 –

崩溃是由于隐含的意图开始使用与GCM旧的集成需要的服务。我相信Google停止了允许Android Lollipop中的服务的隐含意图,因此一旦碰到目标SDK版本,它可能会开始崩溃,因为应用程序不再以兼容模式运行。

我会建议更新到最新版本的Urban Airship。自3.x以来很多变化,所以它可能是一个相当大的迁移。第一步是删除清单条目和jar,然后按照Android Platform guide集成最新版本。