应用不再有效时接收推送通知

时间:2016-10-26 14:22:14

标签: android firebase-cloud-messaging

如果应用不再有效(即使在最近的任务中),如何从FCM接收推送通知。我看到gmail,whatsapp有这个功能。我正在使用FCM进行推送通知。我使用WakefulBroadcast接收器来唤醒设备。我该如何使用?

服务类:

@Override
    public void onMessageReceived(RemoteMessage remoteMessage) {

        // [START_EXCLUDE]
        // There are two types of messages data messages and notification messages. Data messages are handled
        // here in onMessageReceived whether the app is in the foreground or background. Data messages are the type
        // traditionally used with GCM. Notification messages are only received here in onMessageReceived when the app
        // is in the foreground. When the app is in the background an automatically generated notification is displayed.
        // When the user taps on the notification they are returned to the app. Messages containing both notification
        // and data payloads are treated as notification messages. The Firebase console always sends notification
        // messages. For more see: 
        // [END_EXCLUDE]

        // TODO(developer): Handle FCM messages here.
        // Not getting messages here? See why this may be: 

        try {
            if (remoteMessage.getData().size() > 0) {
                Log.d(TAG, "Message data payload: " + remoteMessage.getData());
            }

            // Check if message contains a notification payload.
            if (remoteMessage.getNotification() != null) {
                Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());
            }
            sendNotification(remoteMessage);
            // Also if you intend on generating your own notifications as a result of a received FCM
            // message, here is where that should be initiated. See sendNotification method below.
        } catch (Exception ex) {
            Log.e(TAG, ex.getMessage());
        }
        // Check if message contains a data payload.

    }

接收器:

public class FCMBroadcastReceiver extends BroadcastReceiver {
    public static final String TAG = FCMBroadcastReceiver.class.getSimpleName();
    @Override
    public void onReceive(Context context, Intent intent) {
        Log.i(TAG+"~~~~~~~", "On Boot Completed");
       // ComponentName comp = new ComponentName(context.getPackageName(),
                //YumigoBidMessagingService.class.getName());
        // Start the service, keeping the device awake while it is launching.

        //startWakefulService(context, (intent.setComponent(comp)));
        //setResultCode(Activity.RESULT_OK);

        Intent intent1 = new Intent(context, YumigoBidMessagingService.class);
        context.startService(intent1);

    }
}

manifest资源配置文件:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package="in.yumigo.yumigovendor"
    android:versionCode="2"
    android:versionName="1.0.1">

    <permission android:name="in.yumigo.yumigovendor.permission.C2D_MESSAGE"
        android:protectionLevel="signature" />
    <uses-permission android:name="in.yumigo.yumigovendor.permission.C2D_MESSAGE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="android.permission.READ_SMS" />
    <uses-permission android:name="android.permission.VIBRATE" />
    <uses-permission android:name="android.permission.RECEIVE_SMS" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.READ_LOGS" />
    <uses-feature android:name="android.hardware.camera"/>
    <uses-feature android:name="android.hardware.location.gps" />
    <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

    <application
        android:name=".YumigoVendorApplication"

        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
 <receiver
            android:name="in.yumigo.yumigovendor.receivers.FCMBroadcastReceiver"
            >
            <intent-filter>

                <action android:name="android.intent.action.BOOT_COMPLETED" />
                <!--<action android:name="android.intent.action.QUICKBOOT_POWERON" />
                <category android:name="android.intent.category.DEFAULT" />-->
               <!-- <category android:name="com.android.recognition" />
                <category android:name="in.yumigo.yumigovendor.receivers" />-->
            </intent-filter>
        </receiver>

1 个答案:

答案 0 :(得分:0)

您的应用程序需要调用一次,并且Play代理会调用您的FCM代码(作为服务实现)。然后,您可以调用NotificationManager来弹出消息。请注意,FCM将为您处理“通知”(其中“通知”与“数据”有效负载区分开)。 HTH,祝你好运。

相关问题