私はstackoverflowでこれについての同様の質問が少ないことを知っています。しかし、答えは私が探しているものではなく、GCMではFCMではありません。アプリを閉じても(FCM)onMessageReceivedを呼び出す方法は?
アプリがフォアグラウンドでもバックグラウンドでも、私はonMessageReceived
に電話することができます。しかし、私が望むのは、アプリが閉じられたり、タスクマネージャからスワイプされたときに呼び出すことです。強制終了ではありません。アプリが閉まっていてもプッシュ通知を受け取ることができますが、私はFirebaseにプッシュ通知を保存しているので、onMessageReceived
と呼んでいます。
私はそれを受け取ってもアプリケーションが閉じられていると、データを保存/書き込みできません(プッシュ通知)できません。私はアプリが閉じられたときにプッシュ通知を保存したい。アプリが閉鎖されているときにonMessageReceived
に電話することができますか?私は以下のコードを投稿して、私が何をしているのかを理解しやすくなります。
MyFirebaseMessagingService.java
public class MyFirebaseMessagingService extends FirebaseMessagingService {
private static final String TAG = "MyAndroidFCMService";
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
createNotification(remoteMessage.getData().get("title"),remoteMessage.getData().get("body"));
Bundle bundle = new Bundle();
bundle.putString("msgBody", remoteMessage.getData().get("body"));
bundle.putString("time", currentDateTimeString);
Intent new_intent = new Intent();
new_intent.setAction("ACTION_STRING_ACTIVITY");
new_intent.putExtra("msg", bundle);
sendBroadcast(new_intent);
//create notification
// createNotification(remoteMessage.getNotification().getBody());
}
private void createNotification(String messageTitle, String messageBody) {
Intent intent = new Intent(this , MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent resultIntent = PendingIntent.getActivity(this , 0, intent,
PendingIntent.FLAG_ONE_SHOT);
Uri notificationSoundURI = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder mNotificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(messageTitle)
.setContentText(messageBody)
.setAutoCancel(true)
.setSound(notificationSoundURI)
.setContentIntent(resultIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, mNotificationBuilder.build());
}
}
MainActivity.java
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setContentView(R.layout.menu_actionbar_tab);
activityReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Bundle bundle = intent.getBundleExtra("msg");
String body = bundle.getString("msgBody");
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
String userID = user.getUid();
Log.d("body", body);
mRef.child("customers").child(userID).child("Notification").push().setValue(body);
if (activityReceiver != null) {
IntentFilter intentFilter = new IntentFilter("ACTION_STRING_ACTIVITY");
registerReceiver(activityReceiver, intentFilter);
}
}
マニフェスト
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<meta-data
android:name="android.support.VERSION"
android:value="25.3.0 " />
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="@string/google_maps_key" />
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
<activity
android:name=".Login.ChooseLoginMethodActivity"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<service android:name=".Notification.MyFirebaseMessagingService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT"/>
</intent-filter>
</service>
</application>
を保存あなたは私のマニフェストのコードを含めましたマニフェストコード – Ancee
@Anceeを表示してくださいすることができます。 – arsenallavigne
あなたのアプリがバックグラウンドで呼び出されたときに、メソッド 'onMessageReceived'が呼び出されるようにするには、' FCM'メッセージに 'notification'キー(' data'キーのみ)が含まれていなければならないことに注意してください。 – NamNH