現在、私はAndroidプロジェクトを作成中です。サービスから実行中のアクティビティでメソッドを呼び出す
これまでのところ、私は特にFirebaseInstanceIdServiceとFirebaseMessagingService、Firebaseを実装している:
public class FirebaseIDService extends FirebaseInstanceIdService {
private static final String TAG = "FirebaseIDService";
private Context context;
@Override
public void onTokenRefresh() {
context = getApplicationContext();
// Get updated InstanceID token.
String refreshedToken = FirebaseInstanceId.getInstance().getToken();
Log.e(TAG, "Refreshed token: " + refreshedToken);
SharedPreferences sharedPreferences = context.getSharedPreferences("token", context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("token", refreshedToken);
editor.commit();
sendRegistrationToServer(refreshedToken);
}
/**
* Persist token to backend.
* @param token The new token.
*/
private void sendRegistrationToServer(String token) {
SendRegistrationKey task = new SendRegistrationKey(context);
task.execute(token);
}
}
public class MessagingService extends FirebaseMessagingService {
private static final String TAG = "MsgService";
/**
* Called when message is received.
*
* @param remoteMessage Object representing the message received from Firebase Cloud Messaging.
*/
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
Log.e(TAG, "From: " + remoteMessage.getFrom());
// Check if message contains a data payload.
if (remoteMessage.getData().size() > 0) {
Log.e(TAG, "Message data payload: " + remoteMessage.getData());
}
// Check if message contains a notification payload.
if (remoteMessage.getNotification() != null) {
Log.e(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.
}
// [END receive_message]
private void sendNotification(RemoteMessage remoteMessage) {
RemoteMessage.Notification notification = remoteMessage.getNotification();
PushNotificationManager PNManager = PushNotificationManager.getInstance(getApplicationContext());
PNManager.buildNotification(notification.getTitle(), notification.getBody());
}
私が達成したい何かは以下の通りです:
アプリケーションがバックグラウンドになったら、通知センターに簡単な通知をしたいだけです。 (これはすでに動作しています)
しかし、アプリケーションがフォアグラウンドにあり、現在実行中の場合、別の動作が必要です。 プッシュ通知を消費し、代わりにアラートを表示したいとします。
私の質問は、サービスから実行中のアクティビティとやり取りする方法、または意図した動作を達成する正しい方法は何ですか? Therは単純な魂でなければなりません、そうですか?事前に
おかげ
サービスからsendBroadcastを登録し、活動中のアプリケーションを実行するための優先mothodチェックの上、放送 – X3Btel