我已经完成了关于推送通知及其正常工作的工作,但是直到api级别4.0。但是在api 4.4的情况下,通知点击不会打开活动....我无法理解答案,我在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);
}
答案 0 :(得分:2)
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());
}
答案 1 :(得分:1)
尝试以下内容:
private final static int NOTIFICATION_ID = 1;
private static NotificationManager mNotificationManager;
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1) {
// API 16 onwards
Notification.Builder builder = new Notification.Builder(context);
builder.setAutoCancel(false)
.setContentIntent(pendingIntent)
.setContentText(context.getString(R.string.notification_text))
.setContentTitle(context.getString(R.string.app_name))
.setOngoing(true)
.setSmallIcon(R.drawable.ic_notifier)
.setWhen(System.currentTimeMillis());
Notification notification = builder.build();
notification.flags |= Notification.FLAG_ONGOING_EVENT | Notification.FLAG_NO_CLEAR;
mNotificationManager.notify(NOTIFICATION_ID, notification);
} else {
// API 15 and earlier
NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
builder.setAutoCancel(false)
.setContentIntent(pendingIntent)
.setContentText(context.getString(R.string.notification_text))
.setContentTitle(context.getString(R.string.app_name))
.setOngoing(true)
.setSmallIcon(R.drawable.ic_notifier)
.setWhen(System.currentTimeMillis());
Notification notification = builder.getNotification();
notification.flags |= Notification.FLAG_ONGOING_EVENT | Notification.FLAG_NO_CLEAR;
mNotificationManager.notify(NOTIFICATION_ID, notification);
}
在适当的位置添加JSON部分。并且不要忘记将 android-support-v4.jar 添加到您的项目中,否则它将无法编译。