更新时间:2023-02-26 22:22:31
是的,有可能在应用程序完全关闭时从Google云消息接收通知".
Yes, it is possible 'to receive notifications from google cloud message when the application is fully closed'.
事实上,广播接收器是GCM用于传递消息的机制. 您需要实现 BroadcastReceiver 并在AndroidManifest.xml中声明
Infact, A broadcast receiver is the mechanism GCM uses to deliver messages. You need to have implement a BroadcastReceiver and declare it in the AndroidManifest.xml.
请参考以下代码段.
AndroidManifest.xml
<receiver
android:name=".GcmBroadcastReceiver"
android:permission="com.google.android.c2dm.permission.SEND" >
<intent-filter>
<!-- Receives the actual messages. -->
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<category android:name="com.google.android.gcm.demo.app" />
</intent-filter>
</receiver>
<service android:name=".GcmIntentService" />
Java代码
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向您的设备发送消息时,BroadcastReceiver
将接收到该消息并调用onReceive()
函数,您可以在其中启动服务以实际为您执行预期的任务.
When GCM delivers a message to your device, BroadcastReceiver
will receive the message and call the onReceive()
function, wherein you may start a service to actually perform the intended task for you.
上面的代码示例使用称为WakefulBroadcastReceiver
的专用BroadcastReceiver
,它可以确保设备在服务正常工作时不会进入睡眠状态.
The above Code example uses a specialized BroadcastReceiver
called WakefulBroadcastReceiver
, which makes sure that the device doesn't go to sleep, while the service is doing its work.
请参阅相同的Android官方页面: https://developer.android. com/google/gcm/client.html
Refer to the Official Android Page for the same: https://developer.android.com/google/gcm/client.html