【问题标题】:Get Android Notification To Appear As Banner获取 Android 通知以显示为横幅
【发布时间】:2015-04-08 18:12:12
【问题描述】:

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

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

我的通知工作正常,但它目前只显示在抽屉内。它不是从抽屉里弹出来的(这是我想要的)。

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

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

【问题讨论】:

  • 图片就是Dialog Activity,你的活动是小Dialog(javabeat.net/dialog-activity-android)。当您的应用程序发送通知时,您可能会触发该通知,您将使用BroadcastReceiver 发送消息,并且在主应用程序(或某些后台服务)内您会收到该广播消息并打开此Dialog Activity。我认为这可以解决问题
  • 谢谢,我现在就去看看

标签: java android notifications push-notification


【解决方案1】:

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

您需要提醒通知。这需要您使用通知生成器 (.setPriority) 将优先级设置为高或最大。

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

【讨论】:

    【解决方案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)
        // ...
    ;
    

    然后,您将归档您想要的内容。

    【讨论】:

      【解决方案3】:

      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.
      

      【讨论】:

        【解决方案4】:

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

        你得到了

        windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);

        然后你想获取根视图,如果你想添加一个视图,请使用方法 addView(view, params)。

        看看这个cool article,它可能会对您的用例有所帮助。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2013-10-18
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-01-07
          • 2019-04-07
          相关资源
          最近更新 更多