获取 Android 通知以显示为横幅 [英] Get Android Notification To Appear As Banner

查看:368
本文介绍了获取 Android 通知以显示为横幅的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经相当广泛地查看了广泛的术语(横幅"、弹出"、通知类型"),但我似乎无法弄清楚我认为"是一个非常常见的问题.因此,如果由于我缺乏术语而遗漏了一个非常明显的解决方案,请提供建议.

I've looked fairly extensively across a wide range of terminology ("banner", "pop down", "notification type") and I can't seem to find clarity on what I 'think' is a very common issue. So if there is a very obvious solution I missed due to my lack of terminology please advice.

问题是:我希望 Android 通知显示为从屏幕顶部下拉的横幅"(如果横幅是错误的词,请告知).我浏览了文档,似乎没有遇到切换这种行为的设置.这是我想要的示例:

The problem is this: I want an Android notification to appear as a "banner" that drops down from the top of the screen (if banner is the wrong word for this please advise). I went through the Docs and didn't seem to across a setting that toggles this behaviour. Here is an example of what I would like:

我有通知,但它目前只显示在抽屉里.它没有从抽屉里弹出(这是我想要的).

I have the notification working but it currently only showing up inside the drawer. It's not popping down from the drawer (which is what I want).

这是我的代码,如果您能建议我如何使它也显示为横幅,我将不胜感激:

Here is my code, if you can advise how I can make it also appear as a banner I would greatly appreciate it:

public void createNotification(Context context, Bundle extras)
{
    NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    String appName = getAppName(this);

    Intent notificationIntent = new Intent(this, PushHandlerActivity.class);
    notificationIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
    notificationIntent.putExtra("pushBundle", extras);

    PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);

    int defaults = Notification.DEFAULT_ALL;

    if (extras.getString("defaults") != null) {
        try {
            defaults = Integer.parseInt(extras.getString("defaults"));
        } catch (NumberFormatException e) {}
    }

    NotificationCompat.Builder mBuilder =
        new NotificationCompat.Builder(context)
            .setDefaults(defaults)
            .setSmallIcon(context.getApplicationInfo().icon)
            .setWhen(System.currentTimeMillis())
            .setContentTitle("NotificationTitle")
            .setTicker(extras.getString("title"))
            .setContentIntent(contentIntent)
            .setAutoCancel(true);

    String messageJson = extras.getString("data");
    JSONObject parsed;
    String message = null;
    try {
        parsed = new JSONObject(messageJson);
        message = parsed.getString("message");
    } catch (JSONException e) {
        e.printStackTrace();
    }

    if (message != null) {
        mBuilder.setContentText(message);
    } else {
        mBuilder.setContentText("Notification");
    }

    String msgcnt = extras.getString("msgcnt");
    if (msgcnt != null) {
        mBuilder.setNumber(Integer.parseInt(msgcnt));
    }

    int notId = 0;

    try {
        notId = Integer.parseInt(extras.getString("notId"));
    }
    catch(NumberFormatException e) {
        Log.e(TAG, "Number format exception - Error parsing Notification ID: " + e.getMessage());
    }
    catch(Exception e) {
        Log.e(TAG, "Number format exception - Error parsing Notification ID" + e.getMessage());
    }

    mNotificationManager.notify((String) appName, notId, mBuilder.build());
}

推荐答案

后台的app基本不会调用onMessageReceived()方法.

The app in background basically doesn't call onMessageReceived() method.

只有当通知没有notification键时,后台应用才会调用onMessageReceived()方法.

Only when notification doesn't have notification key, the app in background call onMessageReceived() method.

所以,不要使用notification键.

{
  "to": "/topics/notice",
  "notification": {
    "title": "title",
    "body": "body"
  },
  "data": {
    "url": "https://your-url.dev"
  }
}

只使用data键.

{
  "to": "/topics/notice",
  "data": {
    "title": "title",
    "body": "body"
    "url": "https://your-url.dev"
  }
}

并将优先级设置为最大.

And set priority to max.

notificationBuilder.setContentTitle(title)
    // ...
    .setPriority(NotificationCompat.PRIORITY_MAX)
    // ...
;

然后,您将存档所需内容.

Then, you'll archive what you want.

这篇关于获取 Android 通知以显示为横幅的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆