我從Android Studio的Android監視器收到該錯誤。當我通過GCM發送推送通知,在真實設備中,並且應用尚未啓動或被迫停止時,會出現此錯誤。昨天一切正常,今天根本不工作(只有當應用程序在後臺或前臺運行時纔有效)。錯誤廣播意圖回調:結果= CANCELED forIntent {act = com.google.android.c2dm.intent.RECEIVE pkg = com.flagg327.guicomaipu(有額外)}
我認爲這可能是一個AndroidManifest
錯誤,但我厭倦了尋找問題,找不到任何東西。
清單
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
package="com.flagg327.guicomaipu">
...
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
...
<!--GOOGLE CLOUD MESSAGE-->
<receiver
android:name="com.google.android.gms.gcm.GcmReceiver"
android:exported="true"
android:permission="com.google.android.c2dm.permission.SEND" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<!-- for Gingerbread GSF backward compat -->
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />
<category android:name="com.flagg327.guicomaipu" />
</intent-filter>
</receiver>
<service
android:name="com.flagg327.guicomaipu.gcm.RegistrationService"
android:exported="false" />
<service
android:name="com.flagg327.guicomaipu.gcm.TokenRefreshListenerService"
android:exported="false">
<intent-filter>
<action
android:name="com.google.android.gms.iid.InstanceID" />
</intent-filter>
</service>
<service
android:name="com.flagg327.guicomaipu.gcm.NotificacionsListenerService"
android:exported="false" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
</intent-filter>
</service>
</aplication>
<permission
android:name="com.flagg327.guicomaipu.C2D_MESSAGE"
android:protectionLevel="signature" />
<uses-permission android:name="com.flagg327.guicomaipu.permission.C2D_MESSAGE" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
TokenRefreshListenerService.java
每天登記 '令牌' 更新。因此,每個使用GCM的Android應用都必須有一個InstanceIDListenerService來管理這些更新。
public class TokenRefreshListenerService extends InstanceIDListenerService{
@Override
public void onTokenRefresh() {
// Launch the registration process.
Intent i = new Intent(this, RegistrationService.class);
startService(i);
}
}
NotificacionsListenerService.java
GCM自動顯示的推送通知,但只有當相關聯的應用程序有一個GCMListenerService
public class NotificacionsListenerService extends GcmListenerService {
@Override
public void onMessageReceived(String from, Bundle data) {
Log.d("A", "onMessageReceived()");
// Do something
}
}
RegistrationService.java
GCM識別Android設備使用情況g註冊卡('令牌')。我的應用程序應該能夠從安裝它的每個Android設備註冊。
public class RegistrationService extends IntentService {
/**
* Constructor
*/
public RegistrationService() {
super("RegistrationService");
}
@Override
protected void onHandleIntent(Intent intent) {
// Generate or download the registration 'token'.
InstanceID myID = InstanceID.getInstance(this);
String registrationToken = null;
try {
// Get the registration 'token'.
registrationToken = myID.getToken(
getString(R.string.gcm_defaultSenderId),
GoogleCloudMessaging.INSTANCE_ID_SCOPE,
null
);
// Subscribe to a topic. The app is able now to receive notifications from this topic.
GcmPubSub subscription = GcmPubSub.getInstance(this);
subscription.subscribe(registrationToken, "/topics/guico_maipu_topic", null);
} catch (IOException e) {
e.printStackTrace();
}
Log.e("Registration Token", registrationToken);
}
}
錯誤
,當我通過Python發送推送通知,將出現此錯誤。
09-13 21:21:44.800 1851-1851/? W/GCM-DMM: broadcast intent callback: result=CANCELLED forIntent { act=com.google.android.c2dm.intent.RECEIVE pkg=com.flagg327.guicomaipu (has extras) }
昨天工作...任何想法?比你的時間。
從此[線程]根據(https://groups.google.com/forum/#!msg/android-gcm/MqVlA3Sj26c/6O4TdU1pm0UJ),當接收應用程序是在停止發生此問題設備上的狀態(例如,通過設置強制停止)。手動啓動時,它只會再次開始接收消息。你也可以檢查這[相關SO帖子](http://stackoverflow.com/questions/11902947/broadcast-intent-callback-result-cancelled-forintent)。 – abielita
Sloved檢查此..https://stackoverflow.com/a/45810771/1993001 –