当 Android 应用程序处于 Foreground 状态时,将调用具有通知数据的 onPushNotificationReceived 侦听器。当处于 Background 或 Termulated 状态时,确实会收到通知,但 onPushNotificationReceived 监听器既不会自动调用,也不会在我点击通知时调用。我正在从 Azure 门户发送推送。
{ “通知”:{ "title":"通知中心测试通知", "body":"这是 Azure 通知中心提供的示例通知。" }, “数据”:{ "属性1":"值1", “财产2”:53 }, “优先级”:“高” }
我正在关注教程:使用 Firebase SDK 版本 1.0.0-preview1(当前 SDK)向 Android 设备发送推送通知教程。 https://learn.microsoft.com/en-us/azure/notification-hubs/android-sdk
安卓操作系统:11
除了上述链接中提到的步骤之外,我还需要做其他事情吗?
确保您的应用程序处于后台或已关闭,然后通知中心会显示通知消息,并且该消息中的任何数据都会传递到因用户点击通知而启动的意图。
启动意图后,您可以使用
getIntent().getExtras();
onMessageReceived
为大多数消息类型提供,如下
当您的应用程序处于后台时发送通知消息。通知被传送到设备的系统 托盘。用户点击通知将打开应用程序启动器 默认。
在后台接收时包含通知和数据负载的消息。通知将传递到设备的系统托盘,数据负载将在启动器 Activity 的意图的附加内容中传递。
参考处理响应和在android中推送通知在您的项目中实现。
您可以在 FirebaseMessaging 实例上使用 .getInitialMessage()
FirebaseMessaging.instance
.getInitialMessage()
.then((RemoteMessage message) {
if (message != null) {
Navigator.pushNamed(context, message.data['view']);
}
});
参考
onPushNotificationReceived 侦听器接收。 查看 Android Studio 中的 logcat,我在发送推送通知后看到一条消息。 因此,我实现了一个NotificationListener,并在我想要在应用程序中做出反应的通知标题上设置了一个过滤器。过滤器中的代码在类中实现,以便重用它来处理前台/后台情况。
我认为这是一个解决方法,我遇到的问题是,在Android设备关闭显示超过大约15分钟(进入dooze模式)后,就不再收到通知了。我在我的NotificationListener的片段下面发布
public class NotificationService extends NotificationListenerService {
public NotificationService()
{
super();
}
@Override
public void onCreate() {
super.onCreate();
}
@Override
public void onNotificationPosted (StatusBarNotification sbn) {
if(sbn != null && sbn.getNotification() != null && sbn.getNotification().extras.getCharSequence(Notification.EXTRA_TITLE) != null)
{
String title = sbn.getNotification().extras.getCharSequence(Notification.EXTRA_TITLE).toString();
if(title.equals("alert"){
// ** Code to react to notification
}
}
}
}