gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 22:25:22 43 4
gpt4 key购买 nike

我正在开发 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/

43 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com