我已经广泛查阅了各种术语(“banner”,“pop down”,“notification type”等),但似乎没有找到清晰的解释,我认为这是一个非常普遍的问题。如果有很明显的解决方案,由于我的术语不足而错过了,请告知。
问题如下: 我希望Android通知以“横幅”的形式出现在屏幕顶部(如果“banner”不是正确的词,请指导)。我查看了文档,似乎没有找到可以切换此行为的设置。以下是我想要的示例:
我已经使通知工作,但它目前仅显示在抽屉中。它没有从抽屉中弹出(这就是我想要的)。
如果您能指导我如何将其显示为横幅,我将不胜感激。
以下是我的代码:
问题如下: 我希望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