以下是负责处理我的应用程序收到的推送通知的功能。它应该在用户点击通知点击后将用户重定向到应用中的某个活动。
当我尝试在Android 2.3.6(API 10)设备上运行应用时,通知会得到完美处理。 但是,当我尝试在AVD(Andriod 4.3(API 18))上运行时,没有任何效果 - 默认的android徽标显示为图标而不是ic_launcher.png,用户不会在点击时重定向到应用程序,通知不会自动隐藏点击。
请注意,我使用if语句来检查安装了应用程序的手机的API版本,以确保推送通知不会受到已弃用的类的影响。
@SuppressWarnings("deprecation")
@SuppressLint("NewApi")
public static void createNotification(Context context, String payload) {
int currentapiVersion = android.os.Build.VERSION.SDK_INT;
if (currentapiVersion >= 11) {
Intent intent = new Intent(context, tabswipe.MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0,intent, 0);
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification n = new Notification.Builder(context)
.setContentTitle("New mail from ggg")
.setContentText("zzzz")
.setSmallIcon(R.drawable.ic_launcher)
.setContentIntent(pendingIntent)
.setAutoCancel(true)
.addAction(R.drawable.ic_launcher, "Call", pendingIntent)
.addAction(R.drawable.ic_launcher, "More", pendingIntent)
.build();
notificationManager.notify(0, n);
}
if (currentapiVersion < 11) {
Intent intent = new Intent(context, tabswipe.MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0,intent, 0);
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(R.drawable.ic_launcher,"New Update from VIIT Perception", System.currentTimeMillis());
notification.flags |= Notification.FLAG_AUTO_CANCEL; // Hide the notification after its selected
notification.defaults |= Notification.DEFAULT_LIGHTS; //adding LED lights to notification
notification.setLatestEventInfo(context, "Updates from VIIT Perception", payload, pendingIntent);
notificationManager.notify(0, notification);
}
}