android - FCM push notification depending on app state
问题描述
I am testing the same app on two different ANDROID devices.
On has version 8.1.0 (Moto G5s), the other has version 7.0 (Galaxy A5).
I am working with Firebase CloudMessaging.
Scenario 1: push sent from Firebase console, app is closed, both devices receive the notification and show the notification on status bar
Scenario 2: push sent from Firebase console, app is started but in background, both devices receive the notification and show the notification on status bar
Scenario 3: push sent from Firebase console, app is started and in foreground, both devices receive the notification but only the 7.1 device shows the notification on status bar.
This is the code in AndroidManifest:
<service
android:name=".activity.MyFirebaseService"
android:exported="false">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
And this is MyFirebaseService file:
public class MyFirebaseService extends FirebaseMessagingService {
@Override
public void onNewToken(String s) {
FirebaseInstanceId.getInstance().getInstanceId()
.addOnCompleteListener(new OnCompleteListener<InstanceIdResult>() {
@Override
public void onComplete(@NonNull Task<InstanceIdResult> task) {
if (!task.isSuccessful()) {
Log.w( "getInstanceId failed", task.getException());
return;
}
// Get new Instance ID token
String token = task.getResult().getToken();
Log.e("My Token",token);
}
});
}
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
Log.d("push recibida","push reecibida "+remoteMessage);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, "channel_id")
.setContentTitle(remoteMessage.getNotification().getTitle())
.setContentText(remoteMessage.getNotification().getBody())
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.setStyle(new NotificationCompat.BigTextStyle())
.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
.setSmallIcon(R.mipmap.ic_launcher)
.setAutoCancel(true);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, notificationBuilder.build());
}
}
I would like to know an explanation of the 3 scenarios and then I would appreciate a way to decide to show or not to show the notification icon depending on the state of the app: 1. not launched 2. launched but in background 3. launched and in foreground
解决方案
推荐阅读
- java - org.apache.openjpa.persistence.PersistenceException:使用分布式事务时无法设置自动提交模式
- javascript - 无法在javascript中获取子元素的文本
- php - 在网格中显示带有子项和产品编号的 Woocommerce 产品类别
- google-apps-script - 范围选择器 Google Sheet Apps 脚本
- sql-server - 如何在 SQL Server 2012 中使用 GROUP BY
- r - R 记者到官员和移民
- .net-core - .NET Core GraphQL - GraphQL 架构不可用,而是显示自省查询
- java - 我想将不可变的 Java 对象作为引用传递,是否有 AtomicReferences 或 Array 或 List 的替代方法?
- datatables - ajax 加载后更新 Datatables 列搜索下拉框
- javascript - window.opener 为空...以前不是