【问题标题】:Android: How to react to notification touch events?Android:如何对通知触摸事件做出反应?
【发布时间】:2011-03-18 17:07:49
【问题描述】:

我通过NotificationManager 显示来自BroadcastReceiver 的通知,但如果用户触摸通知,则没有任何反应。如果我从视图创建通知,则在触摸通知时将包含的活动置于最前面。

如何显示特定活动/视图或以其他方式对用户触摸通知做出反应?

【问题讨论】:

  • 在 Eclipse 中使用 adb logcat、DDMS 或 DDMS 透视图查看 LogCat 并查看点击 Notification 时显示的消息。

标签: android notifications broadcastreceiver notificationmanager


【解决方案1】:

当您收到来自 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);

干杯

【讨论】:

    【解决方案2】:

    将此添加到您的清单文件中

    android:name=".QuadDealsPushReceiver"
    
     <application android:label="@string/app_name" android:name=".QuadDealsPushReceiver"
      android:theme="@style/MyTheme" android:debuggable="true">
    

    然后创建新的 Activity 并粘贴此代码。现在,如果您触摸通知,它将重定向到 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!");
            }
        });
    }
    

    }

    【讨论】:

    • @Venaktesh:我相信您将此答案粘贴到错误的问题中,因为它与通知或 OP 询问的任何其他内容无关。
    猜你喜欢
    • 2013-05-14
    • 1970-01-01
    • 1970-01-01
    • 2014-11-13
    • 2014-05-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-27
    相关资源
    最近更新 更多