gpt4 book ai didi

android - Android O中如何显示来自后台服务的推送通知//如何正确使用startForeground()

转载 作者:行者123 更新时间:2023-11-29 15:36:48 25 4
gpt4 key购买 nike

我正在尝试显示由 GCM 消息触发的推送通知。GCM 消息由后台的广播接收器接收,但未显示推送通知。

出于演示目的,我缩小了我的代码:

BroadcastReceiver(日志记录告诉我调用了 onReceive())

public class GcmBroadcastReceiver extends BroadcastReceiver {
private static final String TAG = GcmBroadcastReceiver.class.getName();

@RequiresApi(api = Build.VERSION_CODES.O)
@Override
public void onReceive(Context context, Intent intent) {
Log.d(TAG, "received GCM intent: "+intent.toString());
ComponentName comp = new ComponentName(context.getPackageName(), GcmIntentService.class.getName());
context.startForegroundService(intent.setComponent(comp));
}
}

应该触发通知的 Intent。日志显示调用了 onHandleIntent()。

@RequiresApi(api = Build.VERSION_CODES.O)
public class GcmIntentService extends IntentService {
private static final String TAG = GcmIntentService.class.getName();

public GcmIntentService() { super("GcmIntentService"); }

@Override
protected void onHandleIntent(Intent intent) {
Log.d(TAG,"received intent");
String channelId = "MyChannelId";
Notification notification = new NotificationCompat.Builder(getApplication(), channelId)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("MyTitle")
.setContentText("MyText")
.build();

NotificationChannel channel = new NotificationChannel(channelId, "foo", NotificationManager.IMPORTANCE_HIGH);
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
manager.createNotificationChannel(channel);

startForeground(1, notification);
manager.notify(1, notification);
}
}

我的 AndroidManifest.xml:

    <receiver
android:name=".cdmclient.GcmBroadcastReceiver"
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.REGISTRATION" />
<category android:name="${applicationId}.cdmclient" />
</intent-filter>
</receiver>

<service android:name=".cdmclient.GcmIntentService" android:exported="false"/>

当应用程序在前台接收到 GCM 事件时,日志显示:

D/de.locked.bob.cdmclient.GcmBroadcastReceiver: received GCM intent: Intent { act=com.google.android.c2dm.intent.RECEIVE flg=0x1000010 pkg=de.locked.bob.debug cmp=de.locked.bob.debug/de.locked.bob.cdmclient.GcmBroadcastReceiver (has extras) }
D/de.locked.bob.cdmclient.GcmIntentService: received intent
E/EnhancedIntentService: binding to the service failed

当应用程序在后台接收到 GCM 事件时,日志显示:

D/de.locked.bob.cdmclient.GcmBroadcastReceiver: received GCM intent: Intent { act=com.google.android.c2dm.intent.RECEIVE flg=0x1000010 pkg=de.locked.bob.debug cmp=de.locked.bob.debug/de.locked.bob.cdmclient.GcmBroadcastReceiver (has extras) }
D/de.locked.bob.cdmclient.GcmIntentService: received intent
W/EnhancedIntentService: Service took too long to process intent: com.google.android.c2dm.intent.RECEIVE App may get closed.

当然,不会显示任何通知。我想我以错误的方式使用了 startForeground(..)startForegroundService(...) 但我目前没有想法。

最佳答案

IntentServices 并不意味着作为前台服务启动(引用:Using startForeground() with an Intent Service)

当推送通知从 GCM 到达时,您是否打算启动服务?您不需要该服务来显示通知;您可以像在服务中那样从 BroadcastReceiver 执行此操作。

关于android - Android O中如何显示来自后台服务的推送通知//如何正确使用startForeground(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48127369/

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