简体   繁体   English

Google Cloud Messaging通知功能?

[英]Google Cloud Messaging notification functions?

Now that I got GCM working, I am looking for a way to do in app functions when a message is received, or notification is clicked. 现在,我已经开始使用GCM,现在我正在寻找一种在收到消息或单击通知时在应用程序功能中进行操作的方法。

Sadly I can not find anything about this. 可悲的是我找不到任何东西。 I do however remember vaguely that these things where possible. 但是,我确实模糊地记得这些事情。

I know this is not an good Stackoverflow question, but I really need an answer to this. 我知道这不是一个很好的Stackoverflow问题,但是我确实需要一个答案。

This can be done extending an IntentService : 这可以通过扩展IntentService来完成:

import com.google.android.gms.gcm.GoogleCloudMessaging;

public class GCMprocess extends IntentService {
  public GCMprocess() { super("GCMIntentService"); }

  @Override
  protected void onHandleIntent(final Intent intent) {
    final GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);

    final String messageType = gcm.getMessageType(intent);
    final Bundle extras = intent.getExtras();

    if ((!extras.isEmpty()) && (GoogleCloudMessaging.MESSAGE_TYPE_MESSAGE.equals(messageType))) {
      // This condition would test whether there's a field named code which is called foo
      if (extras.getString("code").equals("foo")) {
        doWhateverYouNeed();
      }
    }

    GCMBroadcastReceiver.completeWakefulIntent(intent);
  }
}

As this is also a Service , you need to register it in your AndroidManifest file: 由于这也是Service ,因此您需要在AndroidManifest文件中注册它:

<service android:name="com.foo.bar.GCMprocess" />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM