gpt4 book ai didi

android - 适用于 Android 4.4 的推送通知服务

转载 作者:搜寻专家 更新时间:2023-11-01 08:53:38 25 4
gpt4 key购买 nike

我已经完成了推送通知的工作,它的工作正常,但以防万一直到 api 级别 4.0。但是在 api 4.4 的情况下,通知点击不会打开 Activity ....我无法理解答案,我在 KitKat 上搜索并使用 Notification.Builder Api 来寻找通知,它给出了相同的结果。

private void generateNotification(Context context, String message, String id) {
int icon = R.drawable.app_icon;
long when = System.currentTimeMillis();
NotificationManager notificationManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);

Notification notification = new Notification(icon, message, when);
String title = context.getString(R.string.app_name);

JSONObject jobj = new JSONObject();

try {
jobj.put("id", id);

} catch (JSONException e) {
e.printStackTrace();
}
System.out.println("json object" + jobj.toString());
Intent notificationIntent = null;

notificationIntent = new Intent(context, JamInfo.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
notificationIntent.putExtra("longi", longi);
notificationIntent.putExtra("lati", lati);


PendingIntent intent = PendingIntent.getActivity(context, 0,
notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);


notification.setLatestEventInfo(context, title, message, intent);


notification.flags |= Notification.FLAG_AUTO_CANCEL;
notification.defaults |= Notification.DEFAULT_VIBRATE;
notification.defaults |= Notification.DEFAULT_LIGHTS;
}

notificationManager.notify((int) System.currentTimeMillis(),
notification);

}

最佳答案

This is working for my apps... Try this...

private void showNotification(Context context) {
// TODO AK-generated method stub
String appName = context.getString(R.string.app_name);
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.notification_icon)
.setContentTitle(appName)
.setContentText(appName);

Uri sound = Uri.parse("android.resource://" + context.getPackageName() + "/raw/" + audioToneName);
mBuilder.setSound(sound);
mBuilder.setAutoCancel(true);
mBuilder.setVibrate(Utility.vibrationPattern);
// Creates an explicit intent for an Activity in your app
Intent resultIntent = new Intent(context, RootActivity.class);
// The stack builder object will contain an artificial back stack for
// the
// started Activity.
// This ensures that navigating backward from the Activity leads out of
// your application to the Home screen.
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
// Adds the back stack for the Intent (but not the Intent itself)
stackBuilder.addParentStack(RootActivity.class);
// Adds the Intent that starts the Activity to the top of the stack
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent =
stackBuilder.getPendingIntent(
0,
PendingIntent.FLAG_UPDATE_CURRENT
);
mBuilder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager =
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
// mId allows you to update the notification later on.
mNotificationManager.notify(321, mBuilder.build());
}

关于android - 适用于 Android 4.4 的推送通知服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20896615/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com