【发布时间】:2016-04-23 20:23:26
【问题描述】:
我研究了一小时又一小时,发现了许多类似的问题,但我的问题没有可行的解决方案。
我明白如果我强制关闭应用程序,这是正常的,我没有收到来自 gcm 的数据。但是在我启动应用程序之前,我无法通过 gcm 收到通知。所以应用程序在前台或后台运行,然后我只能从 gcm 接收数据,但是当我通过滑动关闭应用程序时(这不是通过强制关闭完成的),通知没有显示,这意味着 gcm '根本收不到任何数据。 .所以这意味着我需要保持活跃,但如果我这样做了,那么就没有必要再使用 gcm 来推送通知了。
这是我的清单
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.khooteckwei.google" >
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<permission android:name="com.example.gcmtesting.google.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<uses-permission android:name="com.example.gcmtesting.google.permission.C2D_MESSAGE" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver
android:name=".GcmBroadcastReceiver"
android:permission="com.google.android.c2dm.permission.SEND" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<category android:name="com.example.gcmtesting.google" />
</intent-filter>
</receiver>
<service android:name=".GcmIntentService" />
</application>
</manifest>
这是wakefulBroadcastReceiver
public class GcmBroadcastReceiver extends WakefulBroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// Explicitly specify that GcmIntentService will handle the intent.
ComponentName comp = new ComponentName(context.getPackageName(),
GcmIntentService.class.getName());
// Start the service, keeping the device awake while it is launching.
startWakefulService(context, (intent.setComponent(comp)));
setResultCode(Activity.RESULT_OK);
}
}
当应用程序处于活动状态时通知工作正常,所以我没有包含用于显示通知的功能。这两页有什么问题吗?
【问题讨论】:
-
当您“滑开它”时,您的设备可能会执行强制停止。这不是正常行为,但至少有一些设备制造商搞砸了并做到了这一点。尝试运行您的应用程序,“将其滑开”,然后在“设置”>“应用程序”中查看该应用程序的页面。如果“强制停止”按钮被禁用,那么您知道“滑开它”会强制停止。
-
谢谢回复。但是按钮是启用的,所以我不认为是那个问题
-
澄清一下,你的意思是说你的应用只有在前台才能收到gcm通知吗?
-
@sept 实际上不是。只要应用程序没有完全通过在任务管理器中滑动或清除任务,无论是前景还是背景。通知到达。如果我只是简单地重启手机而不启动应用程序,我将不会收到任何 gcm 通知。
-
@sept er.. gcm 在模拟器中工作正常,但在真实设备中我已经测试过 Oppo N7,三星 s7 都不适用于我
标签: android google-cloud-messaging