获取Android通知以显示为横幅

时间:2015-04-08 18:12:12

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

4 个答案:

答案 0 :(得分:2)

后台应用基本上不调用onMessageReceived()方法。

仅当通知没有notification键时,应用程序才会在后台调用onMessageReceived()方法。

因此,请勿使用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"
  }
}

并将优先级设置为最大。

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

然后,您将存档所需的内容。

答案 1 :(得分:1)

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

你想要一个单挑通知。这要求您将Notifications Builder(.setPriority)的优先级设置为high或max。

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

答案 2 :(得分:0)

您基本上需要了解WindowManager的工作原理。

你得到了

windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);

然后,如果要添加视图,则需要获取根视图并使用方法addView(view,params)。

看看这个cool article,这可能有助于您的用例。

答案 3 :(得分:0)

Orion Granatir是对的。 对于Xamarin.Android开发人员,这里是代码:

        //Create the notification
        var notification = new Notification(Resource.Drawable.icon, title);

        //Auto-cancel will remove the notification once the user touches it
        notification.Flags = NotificationFlags.AutoCancel;
        notification.Sound = RingtoneManager.GetDefaultUri(RingtoneType.Notification);
        notification.Defaults = NotificationDefaults.Vibrate | NotificationDefaults.Lights;
        notification.BigContentView = new Android.Widget.RemoteViews(AppSettings.Instance.PackageName, Resource.Layout.notification_template_big_media);
        notification.LargeIcon = BitmapFactory.DecodeResource(Resources, Resource.Drawable.icon);
        notification.Priority = (int)Android.App.NotificationPriority.Max;//A notification that is at least Notification.PriorityHigh is more likely to be presented as a heads-up notification.