将安卓通知显示为横幅

3
我已经广泛查阅了各种术语(“banner”,“pop down”,“notification type”等),但似乎没有找到清晰的解释,我认为这是一个非常普遍的问题。如果有很明显的解决方案,由于我的术语不足而错过了,请告知。
问题如下: 我希望Android通知以“横幅”的形式出现在屏幕顶部(如果“banner”不是正确的词,请指导)。我查看了文档,似乎没有找到可以切换此行为的设置。以下是我想要的示例:
我已经使通知工作,但它目前仅显示在抽屉中。它没有从抽屉中弹出(这就是我想要的)。
如果您能指导我如何将其显示为横幅,我将不胜感激。
以下是我的代码:
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中(http://www.javabeat.net/dialog-activity-android/)。当你的应用程序发送通知时,你可以触发它,使用`BroadcastReceiver`发送消息,在主应用程序(或某些后台服务)中接收广播消息并打开此`Dialog Activity`。我认为这可以解决问题。 - Jemshit Iskenderov
谢谢,我现在就去看看。 - Aggressor
4个回答

1

在后台运行的应用程序基本上不会调用 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

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.

-1

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

你可以使用以下代码:

windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);

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

你可以参考这篇很棒的文章,它可能有助于你的使用情况。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接