NotificationManager
from a経由で通知を表示してBroadcastReceiver
いますが、ユーザーが通知に触れても何も起こりません。ビューから通知を作成すると、通知に触れると、含まれているアクティビティが前面に表示されます。
特定のアクティビティ/ビューを表示したり、通知に触れたユーザーに反応したりするにはどうすればよいですか?
NotificationManager
from a経由で通知を表示してBroadcastReceiver
いますが、ユーザーが通知に触れても何も起こりません。ビューから通知を作成すると、通知に触れると、含まれているアクティビティが前面に表示されます。
特定のアクティビティ/ビューを表示したり、通知に触れたユーザーに反応したりするにはどうすればよいですか?
NotificationManagerからブロードキャストメッセージを受信したら、ユーザーが適切な通知をタッチしたときに起動される新しいPendingIntentを作成する必要があります。ここで、PendingIntentには、アクティビティの開始など、実行するアクションを持つ内部インテントが必要です。
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);
乾杯
これをマニフェストファイルに追加します
android:name=".QuadDealsPushReceiver"
<application android:label="@string/app_name" android:name=".QuadDealsPushReceiver"
android:theme="@style/MyTheme" android:debuggable="true">
次に、新しいアクティビティを作成し、このコードを貼り付けます。通知をタップすると、Say YourActivityname.class にリダイレクトされます。
public class QuadDealsPushReceiver extends Application {
public static String apid=null;
public void onCreate(){
AirMail am = AirMail.getInstance();
am.acceptPush(this, new PushReceiver() {
@Override
public void onReceive(String message, String payload){
Log.d("push", "Got message '" + message +"' and payload '" + payload + "'");
}
@Override
public void onClick(String message, String payload){
Intent intent = new Intent("android.intent.action.MAIN");
intent.setClass(QuadDealsPushReceiver.this, YourActivityname.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
QuadDealsPushReceiver.this.startActivity(intent);
}
});
am.setAPIDReceiver(this, new APIDReceiver() {
@Override
public void onReceive(String apids, boolean valid){
apid=apids;
if(valid){
Log.d("push", "Got apid: " + apid);
} else {
Log.d("push", "Application registration invalid!"+ apid);
}
}
@Override
public void onAirMailInstallRefusal() {
QuadMain.register = false;
Log.d("push", "AirMail Install Refused!");
}
});
}
}