首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >应用程序未打开时显示提示通知

应用程序未打开时显示提示通知
EN

Stack Overflow用户
提问于 2017-09-25 10:58:37
回答 2查看 3.7K关注 0票数 4

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

EN

回答 2

Stack Overflow用户

发布于 2017-09-25 11:27:58

医生说:

在安卓5.0 (

level 21)中,当设备处于活动状态(即设备解锁且屏幕打开)时,通知可以出现在一个小的浮动窗口(也称为提醒通知)中。这些通知看起来类似于紧凑形式的通知,不同之处在于提示通知还显示了操作按钮。用户可以在不离开当前应用程序的情况下对提醒通知采取行动或解除通知。

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

代码语言:javascript
复制
notificationBuilder.setPriority(Notification.PRIORITY_HIGH);
if (Build.VERSION.SDK_INT >= 21) notificationBuilder.setVibrate(new long[0]);

不要滥用提醒。有关何时使用提醒通知,请参阅here

MAX:用于关键和紧急通知,提醒用户时间紧迫或需要解决才能继续执行特定任务的情况。

HIGH:主要用于重要的通信,例如包含用户特别感兴趣的内容的消息或聊天事件。高优先级通知会触发提示通知显示。

来自HERE的补充说明

更新:

要覆盖GCM侦听器服务,请执行以下操作:

代码语言:javascript
复制
<service android:name=".MyGcmListenerService"
            android:exported="false">
            <intent-filter>
                <action android:name="com.google.android.c2dm.intent.RECEIVE" />
            </intent-filter>
</service>

FCM将为:

代码语言:javascript
复制
<intent-filter>
                <action android:name="com.google.firebase.MESSAGING_EVENT" />
            </intent-filter>

然后重写方法:

GCM :

代码语言:javascript
复制
public class MyGcmListenerService
        extends GcmListenerService {
@Override
public void onMessageReceived(String from, Bundle data) {
    ... create your heads-up notification here.
}

FCM :

代码语言:javascript
复制
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.
}
票数 1
EN

Stack Overflow用户

发布于 2017-09-25 15:00:08

不能发表评论,所以在这里。试试这个,我已经测试过了:

代码语言:javascript
复制
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());
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46396991

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档