android - 如何检测 NotificationManager 发送的通知的发送者/检测点击了通知栏上的哪个通知?

标签 android notifications notificationmanager

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

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

The problem is how to detect the user click on the notification for inside zone or outside zone? (I want to show different view on different case)

Is it possible to add a listener for notification clicked?

这是我的代码 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"); 

}

最佳答案

Is it possible to add a listener for notification clicked?

对每种情况使用不同的 PendingIntent。如果您只是改变一个额外的,请确保在创建 PendingIntent 时使用 FLAG_UPDATE_CURRENT

关于android - 如何检测 NotificationManager 发送的通知的发送者/检测点击了通知栏上的哪个通知?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6112733/

相关文章:

java - android.app.RemoteServiceException : at android. app.ActivityThread$H.handleMessage 发送通知时

android - 通知将无法使用通知管理器和 notification.builder

java - 触摸其他通知时重新发送已取消的通知。安卓

android - Android 上的 Proguard 和 Netty 5

android - 为什么我的代码没有在 Android 8+ (Oreo+) 中启动我的应用程序的固定快捷方式?

android - AndroidManifest 中未解析的类 ParseFirebaseInstanceIdService

android - 改造异步调用和屏幕旋转

ios - 删除之前发布的通知

Android 在 3 小时后设置通知

java - 从通知栏调用工作类