4

当我通过 node-pushnotification 发送 GCM 通知时,我在我的 android 模拟器上以调试模式收到通知。

当我单击通知(应用程序在后台运行)时,它会打开应用程序但不会调用 onNotification。我已经尝试过 popInitialNotification 真假,没有骰子。

我已经设置了一个单独的pushNotification.js文件,index.js以确保它立即设置,然后我的组件通过默认导出使用:

import PushNotification from 'react-native-push-notification';

let queued = [];
let config = null;

const queue = (fn) => (...args) => {
  if (!config) {
    queued.push([fn, args])
  }
  else {
    fn(...args);
  }
};

const releaseQueue = () => {
  queued.forEach(queueItem => queueItem[0](...queueItem[1]));
  queued = [];
};

PushNotification.configure({
  onNotification: queue((notification) => {
    config.onNotification(notification)
  }),
  onRegister: queue((...args) => {
    config.onRegister(...args)
  }),
  senderID: 'mySenderId',
  popInitialNotification: true
});

export default (c) => {
  config = c;
  releaseQueue()
};

我已经浏览了与此问题相关的 react-push-notification 上每个问题的每个线程。我什至尝试像这样设置一个意图过滤器,这样也许我至少可以在我的初始道具中包含推送信息。没有骰子:

    <intent-filter>
      <action android:name="OPEN_MAIN_ACTIVITY" />
      <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>

@Override
protected ReactActivityDelegate createReactActivityDelegate() {
    return new ReactActivityDelegate(this, getMainComponentName()) {
        private JSONObject getPushData(String dataString) {
            try {
                return new JSONObject(dataString);
            } catch (Exception e) {
                return null;
            }
        }

        @Override
        protected Bundle getLaunchOptions() {
            Intent mainIntent = getIntent();
            String dataValue = "";
            Bundle initialProps = new Bundle();
            if (mainIntent != null) {
                Bundle bundle = mainIntent.getExtras();

                if (bundle != null) {
                    JSONObject data = getPushData(bundle.getString("data"));
                    if (data != null) {
                        try {
                            dataValue = data.toString();
                        } catch (Exception e) {
                            // no-op
                        }
                    } else {
                    }
                }
            }
            initialProps.putString("pushData", dataValue); // Read this inside your Root component in React native
            return initialProps;
        }
    };
}

让这件事发挥作用是多么困难啊。

我的服务器端代码:

const PushNotifications = new require('node-pushnotifications');

const push = new PushNotifications({
  gcm: {
    id: 'my id'
  }
})

  await push.send([token], {
    title: 'My Title',
    body: 'body',
    topic: 'com.myapp',
    custom: {
      myCustomThing: 'myCustomThing
    },
    clickAction: "OPEN_MAIN_ACTIVITY",
  });
4

1 回答 1

0

也许您需要将此行添加到androidManifest

android:launchMode="singleInstance"
于 2020-04-09T07:01:28.910 回答