从通知的数据发送到活动并做出相应的行为活动 [英] send data from notification to activity and make activity behave accordingly
问题描述
我有两个类型的通知,下载和上传的通知。
它将通过单击通知打开一个活动,而且活动有两个片段选项卡,用于下载一个,用于上传之一。
我要的是,当点击上传的通知,它不仅开始活动,但打开特定的标签,在下载部分同样的事情。
I have two types of notifications, downloading and uploading notifications. It will open a Activity by clicking the notification, and the Activity has two fragment tabs, one used for downloading and one used for uploading. What I want is, when click the uploading notification, it not only start the Activity but open the specific tab, same thing with the download part.
下面是我做了什么。
// configure the intent
Intent dIntent = new Intent(this, TransferActivity.class);
dIntent.putExtra(WidgetUtils.NOTIFICATION_MESSAGE_KEY, WidgetUtils.NOTIFICATION_OPEN_DOWNLOAD_TAB); // open download task tab
dIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent dPendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, dIntent, PendingIntent.FLAG_UPDATE_CURRENT);
downloadNotification = new Notification(R.drawable.notification_bar_downloading, getString(R.string.notification_bar_title_download_started), System.currentTimeMillis());
downloadNotification.flags |= Notification.FLAG_AUTO_CANCEL;
downloadNotification.contentView = new RemoteViews(getApplicationContext().getPackageName(), R.layout.notification_bar_progress_layout);
downloadNotification.contentIntent = dPendingIntent;
Intent uIntent = new Intent(this, TransferActivity.class);
uIntent.putExtra(WidgetUtils.NOTIFICATION_MESSAGE_KEY, WidgetUtils.NOTIFICATION_OPEN_UPLOAD_TAB); // open upload task tab
uIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent uPendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, uIntent, PendingIntent.FLAG_UPDATE_CURRENT);
uploadNotification = new Notification(R.drawable.notification_bar_uploading, getString(R.string.notification_bar_title_upload_started), System.currentTimeMillis());
uploadNotification.flags |= Notification.FLAG_AUTO_CANCEL;
uploadNotification.contentView = new RemoteViews(getApplicationContext().getPackageName(), R.layout.notification_bar_progress_layout);
uploadNotification.contentIntent = uPendingIntent;
notificationManager = (NotificationManager) getApplicationContext().getSystemService(getApplicationContext().NOTIFICATION_SERVICE);
我处理传递的数据在这样的活动。
I process the passed data in Activity like this
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
onNewIntent(getIntent());
}
@Override
protected void onNewIntent(Intent intent) {
Bundle extras = intent.getExtras();
if (extras != null) {
if (extras.containsKey(WidgetUtils.NOTIFICATION_MESSAGE_KEY)) {
// extract the extra-data in the Notification
String msg = extras.getString(WidgetUtils.NOTIFICATION_MESSAGE_KEY);
if (msg.equals(WidgetUtils.NOTIFICATION_OPEN_DOWNLOAD_TAB)) {
currentPosition = 0;
indicator.setCurrentItem(0);
} else if (msg.equals(WidgetUtils.NOTIFICATION_OPEN_UPLOAD_TAB)) {
currentPosition = 1;
indicator.setCurrentItem(1);
}
}
}
}
但问题是,它总是开始上传标签无论我点击哪种类型的通知。这实在是令人困惑的。结果
任何人都可以帮忙吗?
先谢谢了。
But the problem is it always start the Upload tab no matter which type of notification I clicked. Which is really confusing.
Anyone can help?
thanks in advance.
推荐答案
我在最近的过去面临类似的问题。一些调查后,我发现, onNewIntent
正在呼吁同意图
。最后我用这工作完全正常,我一个解决方法。我将分享与您合作。
I faced similar problem in the recent past. After few investigation, I found that onNewIntent
was being called on same Intent
. I ended up using a workaround which worked perfectly fine for me. I will share that with you.
- 而不是调用的
TransferActivity
到意图
,引入一个中间人NotificationChannelActivity
。在通知所有点击次数将被信道这种中间人。 - 通过提取
意图
在中间人传递的数据并传递给TransferActivity
。可以使用getIntent
API提取数据,然后在重新包装一个新的意图
并揭开序幕TransferActivity
其中,数据的实际处理将完成。 -
在`的PendingIntent使用独有identitier:
- Instead of calling
TransferActivity
throughIntent
, introduce a middle manNotificationChannelActivity
. All clicks on notification will then be channelized to this middle man. - Extract the data passed through
Intent
in middle man and pass them toTransferActivity
. You can extract data usinggetIntent
API and then repack it in a newIntent
and kick offTransferActivity
where the actual handling of data will be done. Use unique identitier in the `PendingIntent:
INT iUniqueId =(INT)(System.currentTimeMillis的()及0xFFFFFFF可);
PendingIntent.getActivity(getApplicationContext(),iUniqueId,uIntent,PendingIntent.FLAG_UPDATE_CURRENT);
int iUniqueId = (int) (System.currentTimeMillis() & 0xfffffff); PendingIntent.getActivity(getApplicationContext(), iUniqueId, uIntent, PendingIntent.FLAG_UPDATE_CURRENT);
这篇关于从通知的数据发送到活动并做出相应的行为活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!