gpt4 book ai didi

android - 强制停止应用程序时接收推送通知。(Android)

转载 作者:太空宇宙 更新时间:2023-11-03 10:43:01 25 4
gpt4 key购买 nike

我正在开发一个使用推送通知 (GCM) 的应用程序,该应用程序在应用程序运行时接收推送通知,但是当我从设置 ->apps->MyApp(单击它)并单击强制停止,然后它不会收到推送通知。我用 WhatsApp 进行了相同的测试,当我强行停止它时它正在接收推送通知。那么我该如何在我的应用程序中实现相同的功能。

注意:在代码中,我在 WakefulBroadcastReceiver 的子类中接收 PushNotifications,我已经在 list 中静态注册了它,即使它在应用程序强制停止时没有被调用。

public class GCM_Receiver extends WakefulBroadcastReceiver {
//Processes Gcm message .
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "GCM_Receiver", Toast.LENGTH_LONG).show();
ComponentName comp = new ComponentName(context.getPackageName(),GCMIntentService.class.getName());
startWakefulService(context, (intent.setComponent(comp)));
setResultCode(Activity.RESULT_OK);
}
}

编辑:我以这种方式静态注册了 GCM_Receiver :

<receiver
android:name="com.myApp.GCM_Receiver"
android:permission="com.google.android.c2dm.permission.SEND" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<action android:name="com.google.android.c2dm.intent.RETRY" />
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />
<category android:name="com.myApp" />
</intent-filter>
</receiver>

编辑:我的 GCMIntentService 代码如下:

public class GCMIntentService extends IntentService{
//Constructor with super().
public GCMIntentService () {
super("GcmIntentService");
}
//Processes gcm messages .
@Override
protected void onHandleIntent(Intent intent) {
Log.d("GCMIntentService ", "GCMIntentService Started");
Toast.makeText(getApplicationContext(), "GCMIntentService Started", Toast.LENGTH_LONG).show();
GCM_Receiver.completeWakefulIntent(intent);
}}

最佳答案

只有 Google Play 服务可以通过 com.google.android.c2dm.permission.SEND 权限调用特定的广播接收器。因为我假设 Play 服务不包括 FLAG_INCLUDE_STOPPED_PACKAGES发送广播时标记,您强制停止的应用程序将不会收到消息。

有趣的是,在强制停止应用程序后我没有收到任何 WhatsApp 消息。不管应用程序如何,如果它可以在强制停止时接收消息,那么它肯定会接收来自带有 FLAG_INCLUDE_STOPPED_PACKAGES 标志的 Intent 的广播。

关于android - 强制停止应用程序时接收推送通知。(Android),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30353087/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com