如何检测NotificationManager发送的通知发件人/检测通知栏上的哪些通知已被点击?

时间:2011-05-24 15:16:34

标签: android notifications notificationmanager

我正在开发GPS应用程序。如果用户进入预定义区域内部或外部,我的应用程序将通过NotificationManager发出通知。我的应用可以针对这两种情况发出通知。

从onResume()开始,我总是得到最新的intent.setExtra()值,而不是被点击通知的intent.setExtra值。

  
    
      

问题是如何检测用户点击内部区域或外部区域的通知? (我想对不同的案例展示不同的观点)

             

是否可以为点击的通知添加侦听器?

    
  

这是我的代码spinet:

private void displayNotificationMessage(String message, boolean vibrate, boolean playSound, Intent contentIntent, String notificationTag)
{       

    Notification notification = new Notification(R.drawable.buslogo, message, System.currentTimeMillis());

    PendingIntent myPendingIntent = PendingIntent.getActivity(this.getBaseContext(),0, contentIntent,PendingIntent.FLAG_UPDATE_CURRENT);

    notification.setLatestEventInfo(this, "Friendly Reminder", message, myPendingIntent);

    contentIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_SINGLE_TOP);

    if (vibrate)
        notification.vibrate=new long[] {100L, 100L, 200L, 500L};

    if (playSound)
        notification.defaults |= Notification.DEFAULT_SOUND;


    notification.number = ++notificationCounter;
    notificationMgr.notify(notificationTag, notificationCounter, notification);

}

@Override
protected void onNewIntent( Intent intent ) {
    Log.i( TAG, "*********** onNewIntent(), intent = " + intent );
    if (intent.getExtras() != null)
    {
        Log.i(TAG, "in onNewIntent = " + intent.getExtras().getString("test"));
    }
    super.onNewIntent( intent );
    setIntent( intent );
}

@Override
public void onResume() {
    super.onResume();
    Log.i(TAG, "*********** Main - onResume()");

    Intent intent = this.getIntent();
    if (intent.getExtras() != null)
    {
        Log.i(TAG, "test = " + intent.getExtras().getString("test"));
    }
}

public void createNotification(String msnContent)
{
    Intent intent = new Intent();
    intent.setClass(this, Main.class);
    Bundle bundle = new Bundle(); 
    bundle.putString("test", msnContent);
    intent.putExtras(bundle);
    displayNotificationMessage(msnContent, true, true, intent, "test"); 

}

2 个答案:

答案 0 :(得分:2)

  

是否可以为点击的通知添加侦听器?

为每种情况使用不同的PendingIntent。如果您只是更换额外内容,请务必在创建FLAG_UPDATE_CURRENT时使用PendingIntent

答案 1 :(得分:0)

我找到了这篇文章的解决方案 - Confusing behavior when starting new activity with PendingIntent。此外,感谢CommonsWare的帮助。

我缺少intent.setAction()。

下面是检测通知栏上点击通知的代码段。希望它可以帮助其他有同样问题的人。

package com.NotificationTest;

import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class Main extends Activity {
    private static final String TAG = Main.class.getSimpleName();
    int notificationCounter =0;
    private NotificationManager notificationMgr;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        notificationMgr =(NotificationManager)getSystemService(
                NOTIFICATION_SERVICE);

        notificationMgr.cancelAll();

        Button b1 = (Button) findViewById(R.id.b1);
        b1.setText("B1");
        b1.setOnClickListener(new OnClickListener() {
            public void onClick(View arg0) {
                createNotification("From B1");
            }
        });


        Button b2 = (Button) findViewById(R.id.b2);
        b2.setText("B2");
        b2.setOnClickListener(new OnClickListener() {
            public void onClick(View arg0) {
                createNotification("From B2");
            }
        });
    }


    private void displayNotificationMessage(String message, boolean vibrate, boolean playSound, Intent contentIntent, String notificationTag)
    {       

        Notification notification = new Notification(R.drawable.icon, message, System.currentTimeMillis());


        contentIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_SINGLE_TOP);

        PendingIntent myPendingIntent = PendingIntent.getActivity(this.getBaseContext(),0, contentIntent,PendingIntent.FLAG_UPDATE_CURRENT);


        notification.setLatestEventInfo(this, "Friendly Reminder", message, myPendingIntent);



        /*if (vibrate)
            notification.vibrate=new long[] {100L, 100L, 200L, 500L};

        if (playSound)
            notification.defaults |= Notification.DEFAULT_SOUND;*/


        notification.number = ++notificationCounter;
        notificationMgr.notify(notificationTag, notificationCounter, notification);

    }

    @Override
    protected void onNewIntent( Intent intent ) {
        Log.i( TAG, "*********** onNewIntent(), intent = " + intent );
        if (intent.getExtras() != null)
        {
            Log.i(TAG, "in onNewIntent = " + intent.getExtras().getString("test"));
        }
        super.onNewIntent( intent );
        setIntent( intent );
    }

    @Override
    public void onResume() {
        super.onResume();
        Log.i(TAG, "*********** Main - onResume()");

        Intent intent = this.getIntent();
        if (intent.getExtras() != null)
        {
            String extraOfClickedNotification = intent.getExtras().getString("test");

            Log.i(TAG, "test = " + extraOfClickedNotification);

            if (extraOfClickedNotification.equals("From B1"))
            {
                // Do something
            }
            else if (extraOfClickedNotification.equals("From B1"))
            {
                // Do something
            }
        }
    }

    public void createNotification(String msgContent)
    {
        Intent intent = new Intent();
        intent.setAction(msgContent + System.currentTimeMillis());
        intent.setClass(this, Main.class);
        Bundle bundle = new Bundle(); 
        bundle.putString("test", msgContent);
        intent.putExtras(bundle);
        displayNotificationMessage(msgContent, true, true, intent, "test"); 

    }

}