应用程序关闭时推送通知,WakefulBroadcastReceiver未运行在android 6中

时间:2017-06-27 21:47:15

标签: android firebase push-notification firebase-cloud-messaging

当我的应用处于前台或后台时,我可以收到推送通知 我添加了FcmBroadcastReceiver扩展WakefulBroadcastReceiver以获得推送 当我关闭我的申请时。 但是,当应用程序关闭时,我仍然没有收到通知。 谢谢你的帮助。

的manifest.xml

<pre>

        <uses-permission android:name="android.permission.WAKE_LOCK" />
        <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
        <permission android:name="<package>.permission.C2D_MESSAGE"
            android:protectionLevel="signature" />
        <uses-permission android:name="com.package.name.permission.C2D_MESSAGE" />
            <receiver
                android:name="<package>.Notification.FcmBroadcastReceiver"
                android:permission="com.google.android.c2dm.permission.SEND">
                <intent-filter>
                    <action android:name="com.google.android.c2dm.intent.RECEIVE" />
                    <category android:name="<package>" />
                </intent-filter>
            </receiver>

<service android:name=".Notification.MyFirebaseMessagingService">
            <intent-filter>
                <action android:name="com.google.firebase.MESSAGING_EVENT" />
            </intent-filter>
        </service>


public class FcmBroadcastReceiver extends WakefulBroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {

        Log.e("receiver","okey");
        // Explicitly specify that GcmIntentService will handle the intent.
        ComponentName comp = new ComponentName(context.getPackageName(),
                MyFirebaseMessagingService.class.getName());
        // Start the service, keeping the device awake while it is launching.
        startWakefulService(context, (intent.setComponent(comp)));
        setResultCode(Activity.RESULT_OK);
    }
}





public class MyFirebaseMessagingService extends FirebaseMessagingService {

    Intent i;
    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        System.out.println("******************** +  getMessageType "+remoteMessage.getMessageType());
        System.out.println("******************** +  getFrom "+remoteMessage.getFrom());
        System.out.println("******************** +  getMessageId "+remoteMessage.getMessageId());
        System.out.println("******************** +  getTo "+remoteMessage.getTo());
        System.out.println("******************** +  getData "+remoteMessage.getData());
        System.out.println("******************** +  getData().get(message) "+remoteMessage.getData().get("message"));
        System.out.println("******************** +  getData().get(title) "+remoteMessage.getData().get("title"));


        showNotification(remoteMessage.getData().get("type"),remoteMessage.getNotification().getTitle(),remoteMessage.getNotification().getBody());




    }



    private void showNotification(String to , String title, String message) {


        i = new Intent(this,ProfileActivity.class);

        i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);


        PendingIntent pendingIntent = PendingIntent.getActivity(this,0,i, PendingIntent.FLAG_UPDATE_CURRENT);

        Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

        NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
                .setAutoCancel(true)
                .setContentTitle(title)
                .setContentText(message)
                .setSound(defaultSoundUri)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentIntent(pendingIntent);


        NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

        manager.notify(0,builder.build());
    }

}

<code>

1 个答案:

答案 0 :(得分:1)

我在您的代码中可以看到的问题是您正在实施FireBase推送通知,但您正尝试以用于实现GCM的方式实现该通知。您不需要设置任何广播接收器,Firebase就是这样做的。您只需运行“FirebaseMessagingService”即可。服务看起来像这样:

public class MyFirebaseMessagingService extends FirebaseMessagingService {
private static final String TAG = "FCM Service";
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    // TODO: Handle FCM messages here.
    // If the application is in the foreground handle both data and notification messages here.
    // Also if you intend on generating your own notifications as a result of a received FCM
    // message, here is where that should be initiated.
    Log.d(TAG, "From: " + remoteMessage.getFrom());
    Log.d(TAG, "Notification Message Body: " + remoteMessage.getNotification().getBody());
}
}

正如您所看到的,它拥有自己的“onMessageReceived”方法,可以处理广播接收器的功能。

查看本教程,它将帮助您理解:

https://www.codementor.io/flame3/send-push-notifications-to-android-with-firebase-du10860kb

注意:如果您希望您的应用在每种情况下工作,即使应用程序处于后台,前台甚至关闭,那么您需要通过通知中的有效负载发送一些数据。然后它将适用于所有条件。要做到这一点,你需要自己的服务器,Firebase不会为此提供便利。