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

时间:2018-01-06 12:33:30

标签: android push-notification google-cloud-messaging android-8.1-oreo foregroundnotification

我试图显示由GCM-Message触发的推送通知。 广播接收器在后台接收GCM消息,但不显示推送通知。

我为了演示目的缩小了我的代码:

BroadcastReceiver(Logging告诉我调用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));
    }
}

应该触发通知的意图。 记录表示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(...),但我目前没有想法。

1 个答案:

答案 0 :(得分:4)

IntentServices并不打算作为Foreground Services启动(参考:Using startForeground() with an Intent Service

当推送通知从GCM到达时,您打算启动服务吗?您不需要该服务来显示通知;你可以像在服务中那样从你的BroadcastReceiver那里做到这一点。