Android 应用程序未打开时,应用程序未收到通知
whatsap等应用程序即使在用户未打开应用程序时也会收到通知。但就我而言,这并没有发生。我收到一个类的通知,该类继承自Android 应用程序未打开时,应用程序未收到通知,android,google-cloud-messaging,broadcastreceiver,Android,Google Cloud Messaging,Broadcastreceiver,whatsap等应用程序即使在用户未打开应用程序时也会收到通知。但就我而言,这并没有发生。我收到一个类的通知,该类继承自GcmListenerService,如下所示 public class GCMPushReceiverService extends GcmListenerService { //This method will be called on every new message received @Override public void onMessageReceived(
GcmListenerService
,如下所示
public class GCMPushReceiverService extends GcmListenerService {
//This method will be called on every new message received
@Override
public void onMessageReceived(String from, Bundle data) {
//Getting the message from the bundle
String message = data.getString("message");
Log.e("msg",message);
//Displaying a notiffication with the message
setNotification(message);
}
}
我发现有类似于gcmbroadcastereceiver
或wakefulbroadcasterreceiver
的东西可能会对我有所帮助。但我对它的实施感到困惑。如果我可以在WakefulBroadcastReceiver
的onReceive()
方法中接收消息,那么我为什么要使用GcmListenerService
?谁能帮助我以正确的方式实施它
我的清单文件如下
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.app">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="com.example.localmarket.permission.C2D_MESSAGE" />
<application
android:name="com.example.app"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
<activity android:name=".SplashActivity"
android:theme="@style/Theme.Transparent"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- GCM Receiver -->
<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.gnirt69.gcmexample" />-->
</intent-filter>
</receiver>
<!-- GCM Receiver Service -->
<service
android:name=".GCMPushReceiverService"
android:exported="false">
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
</intent-filter>
</service>
<!-- GCM Registration Intent Service -->
<service
android:name=".GCMRegistrationIntentService"
android:exported="false">
<intent-filter>
<action android:name="com.google.android.gms.iid.InstanceID" />
</intent-filter>
</service>
</application>
</manifest>
我认为fcm应该有一些后端更改。因此,我现在无法迁移到fcm。可能重复尝试迁移到fcm,它很容易使用,而且非常简单。我认为应该对fcm进行一些后端更改。因此,我现在不能使用它。那么你能帮我解决这个问题吗?GCM和FCM应用服务器是一样的。您唯一可以更改的是向其发出请求的url,但您甚至不必更改。在客户端开始使用FCM时,不需要进行后端更改。服务器端的GCM可以向使用FCM的客户端发送消息。