【问题标题】:Show Heads up notification when app is not open应用未打开时显示抬头通知
【发布时间】:2017-09-25 15:00:07
【问题描述】:

我使用 fcm,当应用程序打开时会显示抬头通知,但在应用程序未打开或被终止时不显示。 应用未打开时如何处理提醒?

【问题讨论】:

  • 目前我知道,FCM 会在收到消息且应用处于后台时自动显示通知。
  • 是的,正常通知显示,但抬头通知不显示。
  • @FakeTonT 你有这个人的解决方案吗?

标签: android firebase firebase-cloud-messaging


【解决方案1】:

医生说:

在 Android 5.0(API 级别 21)中,通知可以以小字体显示 设备时的浮动窗口(也称为提示通知) 处于活动状态(即设备已解锁且其屏幕已打开)。 这些通知看起来类似于您的紧凑形式 通知,除了提示通知还显示操作 纽扣。用户可以在没有提示的情况下对提示通知采取行动或解除 离开当前应用。

根据 Doc,如果您想要提醒通知,您必须创建自己的通知,如下所示:

notificationBuilder.setPriority(Notification.PRIORITY_HIGH);
if (Build.VERSION.SDK_INT >= 21) notificationBuilder.setVibrate(new long[0]);

不要滥用提醒通知。请参阅here 了解何时使用提醒通知:

MAX:用于提醒用户注意的重要和紧急通知 时间紧迫或需要先解决的情况 可以继续执行特定任务。

HIGH:主要用于重要的交流,例如消息或聊天 具有用户特别感兴趣的内容的事件。 高优先级通知触发平视通知显示。

来自HERE的补充说明

更新:

要覆盖 GCM 侦听器服务:

<service android:name=".MyGcmListenerService"
            android:exported="false">
            <intent-filter>
                <action android:name="com.google.android.c2dm.intent.RECEIVE" />
            </intent-filter>
</service>

FCM 将是:

<intent-filter>
                <action android:name="com.google.firebase.MESSAGING_EVENT" />
            </intent-filter>

然后覆盖方法:

GCM:

public class MyGcmListenerService
        extends GcmListenerService {
@Override
public void onMessageReceived(String from, Bundle data) {
    ... create your heads-up notification here.
}

FCM:

public class MyFirebaseMessagingService extends FirebaseMessagingService {

    /**
     * Called when message is received.
     *
     * @param remoteMessage Object representing the message received from Firebase Cloud Messaging.
     */    
    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
    ... create your heads-up notification here.
}

【讨论】:

  • 我试试这个,但是当应用程序没有打开时。通知仅在系统托盘中显示,提示通知不显示。
  • @FakeTonT 是的,您必须从 FCM/GCM 覆盖现有接收器。默认通知不显示为提示。
  • 是的,我输入了 onMessageReceived,但是当应用程序未打开时,单挑不显示,仅在应用程序打开时显示。我想念什么?我的手机是android android 6.0.1
  • @FakeTonT 然后你应该展示你的代码是如何实现的。如果您收到 FCM 消息,则会显示提醒通知。
  • @FakeTonT 你知道你的代码“.setOnlyAlertOnce(true)” ??。这意味着它只会发出一次警报,直到它被删除现有的。设置为 false,然后再次尝试测试您的代码。
【解决方案2】:

无法发表评论,所以在这里。试试这个,我测试过:

private void test() {
    Intent intent;
    intent = new Intent(this, SplashScreenActivity.class);
    Bundle bundle = new Bundle();
    bundle.putBoolean("isDisplayAlert", true);
    bundle.putString("NOTIFICATION_DATA", "data");
    intent.putExtras(bundle);
    PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(),
            new Random().nextInt(), intent,
            PendingIntent.FLAG_UPDATE_CURRENT);

    Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    NotificationCompat.Builder notificationBuilder = (NotificationCompat.Builder) new NotificationCompat.Builder(this)
            .setSmallIcon(R.drawable.ic_location)
            .setContentTitle("Title")
            .setContentText("Body")
            .setAutoCancel(true)
            .setSound(defaultSoundUri)
            .setOnlyAlertOnce(true)
            .setFullScreenIntent(pendingIntent, true);

    NotificationManager notificationManager =
            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

    notificationBuilder.setPriority(Notification.PRIORITY_HIGH);
    notificationBuilder.setVibrate(new long[0]);
    notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
}

【讨论】:

  • 我通过控制台 Firebase 测试并拍摄到我的手机或鸸鹋。它同样发生抬头通知不显示,但只显示正常通知。
  • imgur.com/a/rHc72我的代码和清单imgur.com/a/3odHb
  • 我想当应用程序处于后台模式时。应用程序不调用 onMessageReceived 并且不显示提示通知但显示正常通知
猜你喜欢
  • 1970-01-01
  • 2021-07-27
  • 2016-02-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多