当您收到来自 NotificationManager 的广播消息时,您必须创建一个新的 PendingIntent,当用户触摸适当的通知时将触发该消息。现在,PendingIntent 必须有一个内部 Intent,它可以执行一个动作,比如启动一个活动。
在 BroadcastReceiver 中调用辅助方法的“onReceive”方法,例如“showProximityAlertNotification”
@Override
public void onReceive(Context context, Intent intent) {
showProximityAlertNotification();
}
//now when the user touch the notification on the notification bar, an activity named //"ItemListActivity" will be launched. You can put an IntentFilter, a Category, an Action
//to perform any different operations within your activity
private void showProximityAlertNotification( ){
String titulo = getString(R.string.notification_proximity_alerts_fired_title);
String tickerText = getString(R.string.notification_proximity_alerts_fired_ticker);
String contextText = getString(R.string.notification_proximity_alerts_fired_content);
int icon = R.drawable.wk_logo;
long when = System.currentTimeMillis();
Notification notification = new Notification(icon, tickerText, when);
//define the actions to perform when user touch the notification
Intent launchApp = new Intent(getApplicationContext(), ItemListActivity.class);
launchApp.putExtra("com.xxxxxxx.xxxxxxxxx.bean.Item", "anyObjectYouWant");
launchApp.setAction( "VIEW_DETAILS_PROPERTY" );
PendingIntent launchNotification = PendingIntent.getActivity(getApplicationContext(), 0, launchApp, 0);
notification.setLatestEventInfo(getApplicationContext(), titulo, contextText, launchNotification);
notificationManager.notify(NOTIFICATION_ID_PROXIMITY_ALERT_FIRED, notification);
}
如果您正在执行新启动的活动并想取消启动的通知,只需执行以下操作:
String notManagerName = Context.NOTIFICATION_SERVICE;
NotificationManager notificationManager = (NotificationManager) getSystemService(notManagerName);
notificationManager.cancel(ProximityAlerts.NOTIFICATION_ID_PROXIMITY_ALERT_FIRED);
干杯