统一获取消息方案

大约 2 分钟

统一获取消息方案

Android 设备品牌丰富,各厂商对于点击通知栏后获取消息的方式不尽相同,华为、荣耀等厂商官方推荐使用 Activity 获取消息,而小米、vivo 等厂商支持通过监听器解析获取消息,详见解析推送消息

开发者可能需针对不同厂商编写多套点击处理代码,能否有一种统一方案适用于各厂商呢?

方案详情

环信建议采用 Activity 这种通用方式在点击通知栏后统一获取消息。

客户端应用通过 MainActivity 中的 onCreate 方法接收数据。

public class MainActivity {
  final static String TAG = "MainActivity";

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getIntentData(getIntent());
  }

  @Override
  public void onNewIntent(Intent intent) {
    super.onNewIntent(intent);
    setIntent(intent);
    getIntentData(intent);
  }

  private void getIntentData(Intent intent) {
    if (null != intent) {
      Bundle bundle = intent.getExtras();
      Log.d(TAG, "Bundle bundle: " + (bundle != null));
      if (bundle != null) {
        Log.d(TAG, "from" + bundle.getString("f"))
        Log.d(TAG, "to" + bundle.getString("t"))
        Log.d(TAG, "message id" + bundle.getString("m"))
        Log.d(TAG, "group id" + bundle.getString("g"))
        Log.d(TAG, "extra" + bundle.getString("e"))
      }
    } else {
      Log.i(TAG, "intent is null");
    }
  }
}

参考