Android 接收 gcm 推送通知的刷新活动
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22252065/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Refreshing activity on receiving gcm push notification
提问by Amrit Pal Singh
How to refreshactivity on receiving gcm push notificationif my app is open. I have an activity which contains listview filled with data from the server. I want to refresh my activity (here adding one more item to listview) , if I receive gcm push notification(which also contains some data).
如果我的应用程序打开,如何在接收 gcm 推送通知时刷新活动。我有一个活动,其中包含填充了来自服务器的数据的列表视图。如果我收到 gcm 推送通知(其中还包含一些数据),我想刷新我的活动(这里再向 listview 添加一项)。
- One alternative is to add timerthat periodically do server requests and update the list adapter data but I don't want these because it will take much resources.
- Do I need to add broadcast receiverwhich will trigger on receiving gcm push which further request for newer server data and update my activity UI?
- 一种替代方法是添加定期执行服务器请求并更新列表适配器数据的计时器,但我不想要这些,因为它会占用大量资源。
- 我是否需要添加将在接收 gcm push 时触发的广播接收器,该接收器进一步请求更新的服务器数据并更新我的活动 UI?
Dear commentors, please read the question carefully, I only need to refresh the list(if app is open and that particular activity is open) else no need for same.
亲爱的评论者,请仔细阅读问题,我只需要刷新列表(如果应用程序已打开且特定活动已打开),否则不需要相同的.
回答by Arthur
Took me a few hours to figure it out. Posting here in case anyone anyone else has the same problem.
我花了几个小时才弄明白。在这里发布以防万一其他人有同样的问题。
The idea is that you have to register your activity as a broadcast receiver. The easiest way to do this is like so:
这个想法是您必须将您的活动注册为广播接收器。最简单的方法是这样的:
//register your activity onResume()
@Override
public void onResume() {
super.onResume();
context.registerReceiver(mMessageReceiver, new IntentFilter("unique_name"));
}
//Must unregister onPause()
@Override
protected void onPause() {
super.onPause();
context.unregisterReceiver(mMessageReceiver);
}
//This is the handler that will manager to process the broadcast intent
private BroadcastReceiver mMessageReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
// Extract data included in the Intent
String message = intent.getStringExtra("message");
//do other stuff here
}
};
The above code goes in the activity that you want to 'listen' for events.
上面的代码进入您想要“监听”事件的活动。
Now, how do we send data to this 'listener'? Go to your push notification handler(or from where you want to update your activity) and when you receive a notification call this function:
现在,我们如何向这个“侦听器”发送数据?转到您的推送通知处理程序(或从您想要更新您的活动的位置),当您收到通知时调用此函数:
// This function will create an intent. This intent must take as parameter the "unique_name" that you registered your activity with
static void updateMyActivity(Context context, String message) {
Intent intent = new Intent("unique_name");
//put whatever data you want to send, if any
intent.putExtra("message", message);
//send broadcast
context.sendBroadcast(intent);
}
When you call the above function, your activity should receive it.
当您调用上述函数时,您的活动应该会收到它。
Note: Your activity must be running/open to receive the broadcast intent
注意:您的活动必须正在运行/打开才能接收广播意图
Note2: I switched to a library called 'otto'. It does actually the same thing but easier, 'broadcasts events' thoughout the app. Here's a link http://square.github.io/otto/
注2:我切换到一个名为“otto”的图书馆。它实际上做同样的事情,但更容易,在整个应用程序中“广播事件”。这是一个链接http://square.github.io/otto/
回答by PaulG
I'm assuming your GCMBroadcastReceiver
is in it's own .java file?
我假设你GCMBroadcastReceiver
在它自己的 .java 文件中?
As far as refreshing an activity, I would also like to know the answer to that question.
至于刷新一个活动,我也想知道那个问题的答案。
But for knowing if a particular activity is active or not, meaning on screen just add a boolean
(call it something like "active") and set it to true
in your activity's onResume()
event, and to false
in the onPause()
event:
但是要知道特定活动是否处于活动状态,这意味着只需在屏幕上添加一个boolean
(将其称为“活动”)并将其设置为true
在您的活动的onResume()
事件中,并false
在onPause()
事件中设置:
protected void onResume()
{
super.onResume();
active = true;;
}
protected void onPause()
{
super.onPause();
active = false;
}
Your active
variable would be a boolean which is global
or static
. This way you know if a particular activity is in "front".
您的active
变量将是一个布尔值,即global
or static
。这样您就可以知道特定活动是否在“前面”。
Hope that helps a bit.
希望那些对你有帮助。
回答by e4c5
The accept answer is indeed correct for the "Refreshing activity on receiving gcm push notification" (I've upvoted it too). But if you only want to update a ListView that's being displayed you don't need a broadcast receiver.
接受答案对于“接收 gcm 推送通知的刷新活动”确实是正确的(我也赞成)。但是,如果您只想更新正在显示的 ListView,则不需要广播接收器。
Your GCM listener service can update the database using a ContentProvider rather than inserting a direct sql query.
您的 GCM 侦听器服务可以使用 ContentProvider 更新数据库,而不是插入直接的 sql 查询。
Then you can rely on the notifyChangemethod on the ContentResolver to do the trick.
然后,您可以依靠ContentResolver上的notifyChange方法来解决问题。
Notify registered observers that a row was updated. To register, call registerContentObserver(). By default, CursorAdapter objects will get this notification. If syncToNetwork is true, this will attempt to schedule a local sync using the sync adapter that's registered for the authority of the provided uri. No account will be passed to the sync adapter, so all matching accounts will be synchronized.
通知注册的观察者某行已更新。要注册,请调用 registerContentObserver()。默认情况下,CursorAdapter 对象将收到此通知。如果 syncToNetwork 为 true,这将尝试使用为提供的 uri 的权限注册的同步适配器来安排本地同步。不会将帐户传递给同步适配器,因此将同步所有匹配的帐户。
回答by Amanni
If your app is already running then try to override the onNewIntent
method
如果您的应用程序已在运行,则尝试覆盖该onNewIntent
方法
回答by fbs419
Seems there is an easier way. In the OnMessageReceived method of the GCM Listener, you can just do the update from there instead of sending the notification. You can use the same code you would have used if processing the notification. If you're doing StartActivity from the listener, you have to use the ActivityFlags.NewTask flag.
似乎有更简单的方法。在 GCM 侦听器的 OnMessageReceived 方法中,您可以从那里进行更新而不是发送通知。您可以使用在处理通知时使用的相同代码。如果您正在从侦听器执行 StartActivity,则必须使用 ActivityFlags.NewTask 标志。
回答by Srneczek
To sum it up in single sentence: If you want to refresh activity, broadcastyour custom event when notification arrivesand registeryour activityas broadcast receiverof that event
概括起来讲的一句话:如果你要刷新活动,广播你的自定义事件时通知到达并登记您的活动作为广播接收该事件的
回答by prasad thangavel
Intent notificationIntent = new Intent(context, MainActivity.class);
notificationIntent.setAction(Long.toString(System.currentTimeMillis()));
PendingIntent.getActivity(context, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);