收到 C2DM 推送通知时更改 Activity 行为
我有一个有效的服务器/客户端解决方案,可以将推送通知发送到我的设备。我的项目的下一步是在 C2DMReceiver
类中调用 onReceive 事件时在活动窗口上显示一个对话框。
由于我是 Android 新手,我不知道如何做到这一点,所以如果有人可以向我解释这一点,我会非常高兴。
基本上我重用了 c2dm 的 chrometophone 应用程序中的类。当我为 logcat 创建日志条目时,将调用 onReceive 事件。由于 C2DMReceiver
是一项服务,如果有新消息,我如何获知我的活动?
我用谷歌搜索了很多,但找不到有效的解决方案...我尝试使用 registerReceiver()
但我很确定我做错了。有人有例子吗?
好的,这是我到目前为止得到的:
Activity
BroadcastReceiver mReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
Log.w(Consts.LOG_TAG_SERVICE, "Test");
}
};
// Called when the activity is first created.
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Log.w(Consts.LOG_TAG_SERVICE, "started");
}
// Called when the activity is restarted
protected void onResume() {
super.onResume();
IntentFilter filter = new IntentFilter();
filter.addCategory("com.mydomain.myapp.CONTEXT");
registerReceiver(mReceiver, filter);
}
// Called when the activity is closed
protected void onPause() {
super.onPause();
unregisterReceiver(mReceiver);
}
C2DMReceiver public class C2DMReceiver extends C2DMBaseReceiver {
public C2DMReceiver() {
super("[email protected]");
// TODO Load dynamic Gmail address
}
@Override
public void onRegistrered(Context context, String registrationId) {
Log.i(Consts.LOG_TAG_SERVICE, registrationId);
// Store the registration id in the preferences
SharedPreferences settings = Prefs.get(context);
SharedPreferences.Editor editor = settings.edit();
editor.putString("deviceRegistrationID", registrationId);
editor.commit();
// TODO: Send ID to server
}
@Override
public void onUnregistered(Context context) {
Log.w(Consts.LOG_TAG_SERVICE, "got here!");
}
@Override
public void onError(Context context, String errorId) {
Log.w(Consts.LOG_TAG_SERVICE, errorId);
}
@Override
protected void onMessage(Context context, Intent intent) {
Log.w(Consts.LOG_TAG_SERVICE, "C2DMReceiver: " + intent.getStringExtra("payload"));
Intent broadcastIntent = new Intent();
broadcastIntent.setAction("com.mydomain.myapp.NEWMESSAGE");
broadcastIntent.putExtra("reading", intent.getStringExtra("payload"));
broadcastIntent.addCategory("com.mydomain.myapp.CONTEXT");
context.sendBroadcast(broadcastIntent);
}
}
这是我得到的全部,但我从未收到自己的广播..有人有一些输入吗?
I have a working server/client solution that sends push notification to my device. The next step for my project would be to show a dialog on the activity window when the onReceive event is called in the C2DMReceiver
class.
As I am new to android, I have no idea how to do this, so I would be really happy if someone could explain this to me.
Basically I reused the classes from the chrometophone Application for c2dm. The onReceive event is called, as I create a log entry for logcat. As the C2DMReceiver
is a service, how to I get informed on my activity if there is a new message?
I googled a lot but couldn't find a working solution... I tried to use the registerReceiver()
but I'm pretty sure I did it wrong. Does anyone have an example?
Ok, so here is what I got so far:
Activity
BroadcastReceiver mReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
Log.w(Consts.LOG_TAG_SERVICE, "Test");
}
};
// Called when the activity is first created.
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Log.w(Consts.LOG_TAG_SERVICE, "started");
}
// Called when the activity is restarted
protected void onResume() {
super.onResume();
IntentFilter filter = new IntentFilter();
filter.addCategory("com.mydomain.myapp.CONTEXT");
registerReceiver(mReceiver, filter);
}
// Called when the activity is closed
protected void onPause() {
super.onPause();
unregisterReceiver(mReceiver);
}
C2DMReceiver
public class C2DMReceiver extends C2DMBaseReceiver {
public C2DMReceiver() {
super("[email protected]");
// TODO Load dynamic Gmail address
}
@Override
public void onRegistrered(Context context, String registrationId) {
Log.i(Consts.LOG_TAG_SERVICE, registrationId);
// Store the registration id in the preferences
SharedPreferences settings = Prefs.get(context);
SharedPreferences.Editor editor = settings.edit();
editor.putString("deviceRegistrationID", registrationId);
editor.commit();
// TODO: Send ID to server
}
@Override
public void onUnregistered(Context context) {
Log.w(Consts.LOG_TAG_SERVICE, "got here!");
}
@Override
public void onError(Context context, String errorId) {
Log.w(Consts.LOG_TAG_SERVICE, errorId);
}
@Override
protected void onMessage(Context context, Intent intent) {
Log.w(Consts.LOG_TAG_SERVICE, "C2DMReceiver: " + intent.getStringExtra("payload"));
Intent broadcastIntent = new Intent();
broadcastIntent.setAction("com.mydomain.myapp.NEWMESSAGE");
broadcastIntent.putExtra("reading", intent.getStringExtra("payload"));
broadcastIntent.addCategory("com.mydomain.myapp.CONTEXT");
context.sendBroadcast(broadcastIntent);
}
}
This is all I got, but I never receive my own broadcast.. Does anyone have some inputs?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这应该可以做到。
然后,您可以在主要活动的 onStart 中处理意图。如果您的活动已经在运行,它将由现有实例处理,如果没有,将启动一个新实例。
This should do it.
You then handle the intent in your onStart in your main activity. If your activity is already running, it will be handled by the existing instance, if not, a new instance will be started.