6

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

即使从后台删除应用程序,我也希望我的应用程序能够接收通知。我正在使用 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;
} }
4

0 回答 0