【发布时间】:2016-03-19 12:15:02
【问题描述】:
我需要在我的应用程序中创建并显示一些自定义推送通知(需要传递一些数据并在单击时打开一些特定的活动)。我做了所有像here描述的事情。
当应用程序运行时,一切都按预期工作。但是当它在后台时,我的服务没有启动,我收到一个推送通知,将我带到应用程序的启动屏幕。
我进行了很多研究,但没有找到对这种行为的解释。 谁能告诉我为什么应用程序在后台时忽略我的服务?
这是在我的清单文件中:
<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>
.......................
我什至尝试扩展我自己的唤醒广播接收器并明确启动服务。这是自定义receiver的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 方法并将一些日志放在那里。所以我知道服务根本没有启动
-
你在清单文件中提到的权限是什么?
-
再次更新,伙计们,我说我做了所有描述的事情。
-
您的问题有解决方案吗?如果是,请帮助我,因为我面临同样的问题。
标签: android google-cloud-messaging gcmlistenerservice