java - 获取 Android 通知以显示为横幅

标签 java android notifications push-notification

我已经相当广泛地研究了各种术语(“横幅”、“弹出式菜单”、“通知类型”),但我似乎无法明确我“认为”是一个非常常见的问题.因此,如果由于我缺乏术语而错过了一个非常明显的解决方案,请提出建议。

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

enter image description here

我有通知工作,但它目前只出现在抽屉里。它不会从抽屉里弹出(这正是我想要的)。

这是我的代码,如果你能告诉我如何让它也显示为横幅,我将不胜感激:

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());
}

最佳答案

参见:http://developer.android.com/design/patterns/notifications.html

你想要一个提醒通知。这需要您使用 Notifications Builder (.setPriority) 将优先级设置为高或最大。

http://developer.android.com/design/patterns/notifications.html#correctly_set_and_manage_notification_priority

关于java - 获取 Android 通知以显示为横幅,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29522254/

相关文章:

react-native - 博览会通知中的 DailyNotificationTrigger 在 Android 设备上不起作用

java - 如何临时禁用 JOptionPane 输入对话框上的“确定”按钮?

java - 使用 Java Reflections 覆盖另一个类的方法

android - 如何修复此 Kodein 错误 : Expression 'kodein' cannot be invoked as a function. 找不到函数 'invoke()'

android - 如何将我的 getEmptyForeignCollection() 对象与我的父对象链接起来?

android - 动态壁纸为 android xml

swift - 使用 registerForLocalNotifications 时 Unresolved external 问题

javascript - 用户收到超过 1 个来自 Node.js 的通知

java - Eclipse:作为项目构建器的 Java 应用程序

java - 如何使用 DozerBeanMapper 将具有对象数组列表的 HashMap(列表是 HashMap 中的值)映射到另一个 HashMap?