Get Android Notification To Appear As Banner

2019-07-20 09:10发布

问题:

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.

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

回答1:

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

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

So, do not use notification key.

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

only use data key.

{
  "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.



回答2:

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

You want a Heads-Up Notifications. This requires you to set the priority with the Notifications Builder (.setPriority) to high or max.

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



回答3:

You basically need to understand how WindowManager works.

You get the

windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);

then you want to get the root view and use the method addView(view, params) if you want to add a view.

Take a look at this cool article, which might help your use case.



回答4:

Orion Granatir is right. For Xamarin.Android developers, here is the code:

        //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.