我需要在我的應用程序中創建並顯示一些自定義推送通知(需要傳遞一些數據並在點擊時打開一些特定的活動)。我做了所有類似於here的描述。爲什麼我的GcmListenerService.onMessageReceived在應用程序未運行時未被調用?
當應用程序運行時,一切都按預期工作。但是當它在後臺時,我的服務沒有啓動,並且我收到推送通知,它會將我帶到應用程序的啓動畫面。
我研究了很多,但沒有找到這種行爲的解釋。 任何人都可以告訴我爲什麼我的服務在應用程序在後臺時被忽略?
這是我的清單文件:
<permission
android:name="com.locdel.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<uses-permission android:name="com.locdel.permission.C2D_MESSAGE" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
......................
<application
android:name=".LocdelApplication"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<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="com.google.android.c2dm.intent.RECEIVE" />
<category android:name="com.locdel" />
</intent-filter>
</receiver>
<service
android:name="com.locdel.gcm.LocdelGcmListenerService"
android:exported="false">
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
</intent-filter>
</service>
<service
android:name="com.locdel.gcm.LocdelIDListenerService"
android:exported="false">
<intent-filter>
<action android:name="com.google.android.gms.iid.InstanceID" />
</intent-filter>
</service>
<service
android:name=".gcm.RegistrationIntentService"
android:exported="false" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
</intent-filter>
</service>
.......................
我甚至試圖擴大自己清醒的廣播接收器,並明確啓動服務。這是自定義的接收機的onReceive方法,它實際上是所謂的,但服務沒有再次啓動:
@Override
public void onReceive(Context context, Intent intent) {
Intent serviceIntent = new Intent(context, LocdelGcmListenerService.class);
serviceIntent.setAction("com.google.android.c2dm.intent.RECEIVE");
serviceIntent.putExtras(intent.getExtras());
startWakefulService(context, serviceIntent);
setResultCode(Activity.RESULT_OK);
}
嗨,你能說明你是如何在你的manifest.xml和服務的代碼中聲明服務的嗎? –
更新了我的問題。代碼服務實際上並不重要。我已經覆蓋onMessageReceived方法並在那裏放置一些日誌。所以我知道服務根本沒有啓動 – shift66
你在清單文件中提到的權限是什麼? – AndiM