Web サーバーからアプリに通知を送信する Web アプリを作成しています。
アプリがバックグラウンドから削除された場合でも、アプリが通知を受け取るようにしたかったのです。通知の受信に 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;
} }