ConnectyCube / connectycube-reactnative-samples

Chat and Video Chat code samples for React Native, ConnectyCube
https://connectycube.com
Apache License 2.0
124 stars 111 forks source link

Push notification using Expo not working #218

Closed xj-m closed 3 years ago

xj-m commented 3 years ago

In the example code, the "react-native-push-notification" is used for notification, which is not available in the expo, so I referred to https://docs.expo.io/push-notifications/sending-notifications-custom/ and here is my code

// import PushNotification from "react-native-push-notification";
import ConnectyCube from "react-native-connectycube";
import store from "../Store";
import { Platform } from "react-native";
import appConfig from "../config.json";
import { AsyncStorage } from "react-native";
import Dialog from "../models/dialogs";
import User from "../models/user";
import { addNewDialog } from "root/store/actions/dialogs.action";
import { fetchUsers } from "root/store/actions/users.action";
import * as Notifications from 'expo-notifications'
import * as Permissions from "expo-permissions";
import Constants from 'expo-constants';

class PushNotificationService {
  static DEVICE_TOKEN_KEY = null;
  static Navigation = null;

  async init(navigation) {
    console.log("[notification init] start");
    const { status } = await Permissions.askAsync(Permissions.NOTIFICATIONS);
    if (status !== "granted") {
      console.log("[notification init] No notification permissions!");
      return;
    }
    // Get the token that identifies this device
    console.log("[Notification init] prepare to get token");
    // const token = await Notifications.getExpoPushTokenAsync();
    const token = (await Notifications.getDevicePushTokenAsync())
    console.log("[Notification init] token ", token);
    PushNotificationService.DEVICE_TOKEN_KEY = token;
    PushNotificationService.Navigation = navigation;
    const settings = {
      onRegister: this.subscribeToPushNotification,
      onNotification: this.onNotification,
      popInitialNotification: true,
      requestPermissions: true,
    };
    await this.subscribeToPushNotification(token)
    if (Platform.OS !== "ios") {
      settings.senderID = appConfig.senderID;
    } else {
      settings.permissions = {
        alert: true,
        badge: true,
        sound: true,
      };
    }
    // PushNotification.configure(settings);
    console.log("[notification init]: setting:", settings)
  }
...

Then I have registered devices in "admin dashboard" > "push notification" > "Device Subscription", but the notification on the "Queue" page shows they failed, the errors are "BadDeviceToken" or "DeviceTokenNotForTopic"

How should I debug this error, and is there an official way on push notification on Expo? Thank you so much!

DaveLomber commented 3 years ago

@xj-m it seems a push device token is wrong

1) first of all, please share an example of iOS device token. 2) make sure you use native device token for IOS rather than FCM-like 3) make sure you are using proper 'environment' for push subscriptions and for push events. Make sure when you subscribe for development env, then you send an event for development env (to for production)

xj-m commented 3 years ago

After set the correct development env, seems "BadDeviceToken" issues is solved,

[notification subscriptions] result Array [
  Object {
    "subscription": Object {
      "device": Object {
        "bundle_identifier": null,
        "client_identification_sequence": "350843....d1a1af61bd2",
        "platform": Object {
          "name": "ios",
        },
        "udid": "E3E*****-****-****-****-********19A8",
      },
      "id": 1488867,
      "notification_channel": Object {
        "name": "apns",
      },
    },
  },
]

This is the log message from ConnectyCube.pushnotifications.subscriptions, my device token(client_identification_sequence) is of length 64, and it seems the udid is also in the right format, but I still get the error "DeviceTokenNotForTopic"

DaveLomber commented 3 years ago

@xj-m another thing to check is make sure you crated Universal iOS push certificate and uploaded it to ConnectyCube admin panel

Please double check it

xj-m commented 3 years ago

image Yes, I followed the instruction in this document https://developers.connectycube.com/reactnative/push-notifications and uploaded to the admin panel

Is that possible that the reason is that the bunder ID is not aligned with the environment, since I'm using expo client, with bundler ID set in config "app.json". As for Testflight, I think I tried once but still got some error.

xj-m commented 3 years ago

After moving to the Testflight, everything works as expected, so I believe it's bunder-id problem

hrabkin commented 2 years ago

@DaveLomber I tried to send push notifications using curl command and logs in dashboard show that it connecty cube server search subscribers ALWAYS in production environment. But shouldn't it search subscribers in environment provided in event params?

DaveLomber commented 2 years ago

@xj-m we recently release some updates to EXPO push notifications this may help https://developers.connectycube.com/reactnative/push-notifications?id=expo

@GRyabkin we made it simpler you do not need to specify the ENV instead, the push will be sanded to ALL envs a user has subscriptions to