我试图在我的应用中实施推送通知,已经使用GCM。但是我希望它能以离线方式工作。 即:对于Android应用程序内的多个条件,它应该触发推送通知。
目前正在尝试:
NotificationManager mNotificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.custom_notification);
contentView.setImageViewResource(R.id.image, R.drawable.ic_launcher);
contentView.setTextViewText(R.id.title, title);
contentView.setTextViewText(R.id.text, msg);
notification.contentView = contentView;
Intent notificationIntent = new Intent(this, HomeActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(this,randomNo, notificationIntent, 0);
但问题是, 1.当应用程序没有运行时,通知到达但是它会使屏幕闪烁(becoz我使用意图调用它)。 2.i想要为多个条件使用相同的类并将其转发到不同的活动(目前我无法根据condtiop将意图更改为另一个活动)。
答案 0 :(得分:2)
1)发送通知的正确方法是mNotificationManager.notify(...)
,例如:
private void sendNotification(String message, String title, int id) {
Intent intent = new Intent(this, HomeActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */,
intent, PendingIntent.FLAG_ONE_SHOT);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_stat_icon)
.setContentTitle(title)
.setContentText(message)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(id /* ID of notification */,
notificationBuilder.build());
}
2)您可以将HomeActivity.class
替换为类构造函数中提供的变量,或将某个方法替换为参数Class<?> cls
,并使用Intent notificationIntent = new Intent(this, cls);