【问题标题】:Change Activity behaviour when C2DM Push notification is received收到 C2DM 推送通知时更改活动行为
【发布时间】:2011-04-14 11:38:00
【问题描述】:

我有一个可以向我的设备发送推送通知的有效服务器/客户端解决方案。我的项目的下一步是在 C2DMReceiver 类中调用 onReceive 事件时在活动窗口上显示一个对话框。

由于我是 android 新手,所以我不知道如何做到这一点,所以如果有人可以向我解释这一点,我会非常高兴。

基本上我重用了 c2dm 的 chrometophone 应用程序中的类。当我为 logcat 创建一个日志条目时,将调用 onReceive 事件。由于C2DMReceiver 是一项服务,如果有新消息,我如何了解我的活动?

我用谷歌搜索了很多,但找不到可行的解决方案...我尝试使用registerReceiver(),但我很确定我做错了。谁有例子?

好的,这就是我目前得到的:

活动

    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);
    }

C2DM接收器 公共类 C2DMReceiver 扩展 C2DMBaseReceiver {

public C2DMReceiver() {
    super("my_test@gmail.com");

    // 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);

}

}

这就是我所得到的,但我从来没有收到我自己的广播。有没有人有一些意见?

【问题讨论】:

  • 为了澄清,到达Log.w(Consts.LOG_TAG_SERVICE, "C2DMReceiver: " + intent.getStringExtra("payload"));这一行,对吧?
  • 是的!我可以看到我用 logcat 推送到设备的内容
  • 我的回答对你有帮助吗?

标签: android push-notification android-c2dm


【解决方案1】:

应该这样做。

@Override
protected void onMessage(Context context, Intent intent) {

  Log.w(Consts.LOG_TAG_SERVICE, "C2DMReceiver: " + intent.getStringExtra("payload"));

  Intent i = new Intent(context, YourMainActivity.class);
  i.putExtra("reading", intent.getStringExtra("payload"));
  i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  context.startActivity(i);

}

然后,您在主要活动中处理 onStart 中的意图。如果您的活动已经在运行,它将由现有实例处理,如果没有,将启动一个新实例。

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2011-09-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-01-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多