notificationReceiver = new NotificationReceiver();
先来看看这个。
这个NotificationReceiver是继承BroadcastReceiver这个类的,这个类还不太了解,去网上搜搜。
http://yangguangfu.iteye.com/blog/1063732这个文章不错
这个是一个接收intent,然后做一些处理工作的类。所谓处理工作,就是一个广播。
重写onReceive方法,定义收到intent之后干嘛:
@Override
public void onReceive(Context context, Intent intent) {
Log.d(LOGTAG, "NotificationReceiver.onReceive()...");//先声明,准备好接收了。。。
String action = intent.getAction();//得到action
Log.d(LOGTAG, "action=" + action);//就是那个service
if (Constants.ACTION_SHOW_NOTIFICATION.equals(action)) {
String notificationId = intent //首先读出各种参数
.getStringExtra(Constants.NOTIFICATION_ID);
String notificationApiKey = intent
.getStringExtra(Constants.NOTIFICATION_API_KEY);
String notificationTitle = intent
.getStringExtra(Constants.NOTIFICATION_TITLE);
String notificationMessage = intent
.getStringExtra(Constants.NOTIFICATION_MESSAGE);
String notificationUri = intent
.getStringExtra(Constants.NOTIFICATION_URI);
Log.d(LOGTAG, "notificationId=" + notificationId);
Log.d(LOGTAG, "notificationApiKey=" + notificationApiKey);
Log.d(LOGTAG, "notificationTitle=" + notificationTitle);
Log.d(LOGTAG, "notificationMessage=" + notificationMessage);
Log.d(LOGTAG, "notificationUri=" + notificationUri);
Notifier notifier = new Notifier(context);
notifier.notify(notificationId, notificationApiKey,
notificationTitle, notificationMessage, notificationUri);
}
}
红色字体是重点,下面看看Notifier类的方法。
public Notifier(Context context) {
this.context = context;
this.sharedPrefs = context.getSharedPreferences(
Constants.SHARED_PREFERENCE_NAME, Context.MODE_PRIVATE);
this.notificationManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
}
这里主要是初始化notifier,然后准备好notificationManager来显示推送信息。
notify方法:
public void notify(String notificationId, String apiKey, String title,
String message, String uri) {
Log.d(LOGTAG, "notify()...");//通知,要开始显示通知了
Log.d(LOGTAG, "notificationId=" + notificationId);
Log.d(LOGTAG, "notificationApiKey=" + apiKey);
Log.d(LOGTAG, "notificationTitle=" + title);
Log.d(LOGTAG, "notificationMessage=" + message);
Log.d(LOGTAG, "notificationUri=" + uri);
if (isNotificationEnabled()) {
// Show the toast
if (isNotificationToastEnabled()) {
Toast.makeText(context, message, Toast.LENGTH_LONG).show();
}
// Notification
Notification notification = new Notification();
notification.icon = getNotificationIcon();
notification.defaults = Notification.DEFAULT_LIGHTS;
if (isNotificationSoundEnabled()) {
notification.defaults |= Notification.DEFAULT_SOUND;
}
if (isNotificationVibrateEnabled()) {
notification.defaults |= Notification.DEFAULT_VIBRATE;
}
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notification.when = System.currentTimeMillis();
notification.tickerText = message;
Intent intent = new Intent(context,NotificationDetailsActivity.class);//跳转到这个界面
intent.putExtra(Constants.NOTIFICATION_ID, notificationId);
intent.putExtra(Constants.NOTIFICATION_API_KEY, apiKey);
intent.putExtra(Constants.NOTIFICATION_TITLE, title);
intent.putExtra(Constants.NOTIFICATION_MESSAGE, message);
intent.putExtra(Constants.NOTIFICATION_URI, uri);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
intent, PendingIntent.FLAG_UPDATE_CURRENT);
notification.setLatestEventInfo(context, title, message,
contentIntent);
notificationManager.notify(random.nextInt(), notification);//显示推送信息
} else {
Log.w(LOGTAG, "Notificaitons disabled.");
}
}
好了,其实就是准备好接受推送消息并显示了。