我正在使用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();
}`
在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。 –
此代碼無效: 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(); } –
嘗試在上述註釋中使用「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。 –