我已经相当广泛地研究了各种术语(“横幅”、“弹出式菜单”、“通知类型”),但我似乎无法明确我“认为”是一个非常常见的问题.因此,如果由于我缺乏术语而错过了一个非常明显的解决方案,请提出建议。
问题是这样的: 我希望 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());
}
最佳答案
参见:http://developer.android.com/design/patterns/notifications.html
你想要一个提醒通知。这需要您使用 Notifications Builder (.setPriority) 将优先级设置为高或最大。
关于java - 获取 Android 通知以显示为横幅,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29522254/