如何在Android Oreo中从后台删除应用程序时收到FCM通知?

时间:2018-01-14 19:07:08

标签: android firebase firebase-cloud-messaging firebase-notifications firebase-job-dispatcher

我正在创建一个网络应用,它会将我的网络服务器的通知发送到应用。

我希望我的应用即使应用程序从后台删除也会收到通知。我正在使用FCM接收通知。

应用程序处于后台时,应用程序会收到通知,但当应用程序从android Oreo中的后台删除时,应用程序不会收到通知。该应用程序接收较低Android版本的通知,即使该应用程序是从后台删除。谢谢你的帮助!

消息服务代码:

public class MyFirebaseMessagingService extends FirebaseMessagingService {

private static final String TAG = "MyFirebaseMsgService";

@Override
public void handleIntent(Intent intent) {
    super.handleIntent(intent);
}

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {

    Map<String,String> data=remoteMessage.getData();
    String title=data.get("title");
    String body=data.get("body");

    if (remoteMessage.getData().size() > 0) {
        Log.e(TAG, "Data Payload: " + remoteMessage.getData().toString());
        if (/* Check if data needs to be processed by long running job */ true) {
            // For long-running tasks (10 seconds or more) use Firebase Job Dispatcher.
            scheduleJob();
        } else {
            // Handle message within 10 seconds
            handleNow();
        }
        try {
            //JSONObject json = new JSONObject(remoteMessage.getData().toString());
            //sendPushNotification(json);
            sendPushNotification(title,body);

        } catch (Exception e) {
            Log.e(TAG, "Exception: " + e.getMessage());
        }
    }
}

//this method will display the notification
//We are passing the JSONObject that is received from
//firebase cloud messaging
public void sendPushNotification(String title,String message) {
    //optionally we can display the json into log
    //Log.e(TAG, "Notification JSON " + json.toString());
    try {
        //getting the json data
        //JSONObject data = json.getJSONObject("data");

        //parsing json data
        /*String title = data.getString("title");
        String message = data.getString("message");
        String imageUrl = data.getString("image");*/
        String imageUrl="";
        //creating MyNotificationManager object
        MyNotificationManager mNotificationManager = new MyNotificationManager(getApplicationContext());
        //creating an intent for the notification
        Intent intent = new Intent(getApplicationContext(), MainActivity.class);
        //if there is no image
        if(imageUrl.equals("null")){
            //displaying small notification
            mNotificationManager.showSmallNotification(title, message, intent);
            mNotificationManager.playNotificationSound();
        }else{
            //if there is an image
            //displaying a big notification
            mNotificationManager.showBigNotification(title, message, imageUrl, intent);
            mNotificationManager.playNotificationSound();
        }

    } catch (Exception e) {
        Log.e(TAG, "Json Exception: " + e.getMessage());
    }
    /*catch (Exception e) {
        Log.e(TAG, "Exception: " + e.getMessage());
    }*/
}
/**
 * Handle time allotted to BroadcastReceivers.
 */
private void scheduleJob() {
    // [START dispatch_job]
    FirebaseJobDispatcher dispatcher = new FirebaseJobDispatcher(new GooglePlayDriver(this));
    Job myJob = dispatcher.newJobBuilder()
            .setService(MyJobService.class)
            .setTag("my-job-tag")
            .setLifetime(Lifetime.FOREVER)
            .setTrigger(Trigger.NOW)
            .setConstraints(Constraint.ON_ANY_NETWORK)
            .setRecurring(true)
            .build();
    dispatcher.mustSchedule(myJob);
    // [END dispatch_job]
}

/**
 * Handle time allotted to BroadcastReceivers.
 */
private void handleNow() {
    Log.d(TAG, "Short lived task is done.");
} }

android清单代码:

    <service android:name=".MyFirebaseInstanceIDService">
        <intent-filter>
            <action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
        </intent-filter>
    </service>
    <service android:name=".MyFirebaseMessagingService">
        <intent-filter>
            <action android:name="com.google.firebase.MESSAGING_EVENT" />
            <action android:name="android.intent.action.RESPOND_VIA_MESSAGE" />
        </intent-filter>
    </service>
    <service android:name=".MyJobService"
        android:exported="false"
        android:enabled="true">
        <intent-filter>
            <action android:name="com.google.firebase.jobdispatcher.ACTION_EXECUTE" />
        </intent-filter>
    </service>

    <meta-data
        android:name="com.google.firebase.messaging.default_notification_icon"
        android:resource="@mipmap/ic_launcher" />
    <meta-data
        android:name="com.google.firebase.messaging.default_notification_color"
        android:resource="@color/customColor" />
    <meta-data
        android:name="com.google.firebase.messaging.default_notification_channel"
        android:value="@string/default_notification_channel_id" />
用于firebase作业调度程序的 Myjobservice代码

public class MyJobService extends JobService {
private static String tag="jobsstarted";
private AsyncTask mBackgroundTask;
@SuppressLint("StaticFieldLeak")
@Override
public boolean onStartJob(JobParameters job) {
    mBackgroundTask = new AsyncTask() {
        @Override
        protected Object doInBackground(Object[] objects) {
            Context context = MyJobService.this;
            startService(new Intent(context, MyFirebaseMessagingService.class));
            return null;
        }
    };
    mBackgroundTask.execute();
    return true;
}@Override
public boolean onStopJob(JobParameters job) {
    return false;
} }

0 个答案:

没有答案
相关问题