I am making a react native app for android devices using socket.io-client. Everything works fine but when the app goes on background mode (e.g. an other app launched) the socket connections disconnects from the server (app emits 'disconnect' event).
What is the best way to deal with it?
In the disconnect event of the socket I reconnect it, if user is not logging out.
socketInstance.on('disconnect', (err) => {
console.log('SOCKET DISCONNECT', err);
var deauthorize = store.getState().auth.account.deauthorize;
if (!deauthorize) {
store.dispatch(NotificationActions.addNotificationAction('SOCKET DISCONNECT'));
store.dispatch(AppActions.shouldConnectAction());
store.dispatch(CommunicationActions.connectSocketAction());
}
});
Related
I am making a React Native app which talks to Spotify via this module: https://github.com/cjam/react-native-spotify-remote
The module has an event listener which allows us to listen to Spotify player state changes, for example:
import { remote as SpotifyRemote } from "react-native-spotify-remote";
// and in a React component:
SpotifyRemote.addListener("playerStateChanged", (state) => {
console.log(state);
});
The above works as long as our RN app is in focus. However, when the app is in the background (i.e. we are on a different app or the phone is locked), the event listner no longer gets triggered.
I am expecting the event listener to get triggered even when the app is running in the background but it isn't doing that.
Is there anyway to make this work even the app is not in focus? Thank you!
I have tried to follow this guide https://rnfirebase.io/messaging/usage/ios-setup to set up push notifications for my React-Native application. In particular, I have done the following:
Adding Push Notification & Background Mode capabilities(with Remote fetch and background activities)
Register a key(with APNs enabled) in Apple developer account and upload it to firebase console settings with the correct KeyID(obtained when registering the key) and TeamID(obtained from developer's membership detail)
Register the App Identifier(with APNs capability). Since there are two bundle Identifiers for my project - org.reactjs.native.example.AppName and org.test.AppName, I have tried both but none works.
Register a Provisioning profile. I believe this wil automatically sync with my Xcode.
I note that I can further configure the APNs capability after registering the App Identifier, but this is not mentioned in the guide and I didn't do that:
To configure push notifications for this App ID, a Client SSL Certificate that allows your notification server to connect to the Apple Push Notification Service is required. Each App ID requires its own Client SSL Certificate. Manage and generate your certificates below.
In my React-Native application, I have the following code:
const App => {
useEffect(() => {
async function requestUserPermission() {
const authStatus = await messaging().requestPermission();
const enabled =
authStatus === messaging.AuthorizationStatus.AUTHORIZED ||
authStatus === messaging.AuthorizationStatus.PROVISIONAL;
if (enabled) {
console.log('Authorization status:', authStatus);
}
}
requestPermission();
});
useEffect(() => {
async function getToken() {
await messaging().registerDeviceForRemoteMessages();
const token = await messaging().getToken();
console.log(token);
}
getToken();
});
...
}
After accepting the notification permission request when the app launch. This will output the FCMs token, which I use to send a test message in the Firebase console.
Did I miss any steps? Is it possible to send push notifications in React-Native debug built running under metro in the first place? Thank you in advance.
I figured out the problem. It is because I used a different bundle ID when building the product in XCode and when registering the identifier in Apple Developer Account. The steps by steps does work as of writing.
I am trying to send data from one android phone to another using "react-native-bluetooth-classic". I successfully run the example in the repo, but I am not able to connect to the paired devices. Sometimes, I can connect to a phone but the other can't connect back.
Is there any configuration I should to the phone's bluetooth?
Merged the pull request last night, should be able to connect from one device to another with the provided BluetoothClassicExample app using the Accept Connection button at the bottom of the device list.
async acceptConnections() {
console.log("App is accepting connections now...");
this.setState({ isAccepting: true });
try {
let connectedDevice = await RNBluetoothClassic.accept();
if (connectedDevice) { // Undefined if cancelled
this.setState({ connectedDevice, isAccepting: false });
}
} catch(error) {
console.log(error);
this.refs.toast.show(
`Unable to accept client connection`,
DURATION.LENGTH_SHORT
);
this.setSTate({ isAccepting: false });
}
}
This is only available on Android as from what I've seen, there are no ways to see the connection on IOS. I was able to connect my Android to my IOS using the example app, but without the Android MFi protocols, the data just gets sent into the IOS void.
I'm trying to log a firebase event when a user is receiving a remote push notification when the app is in the background/killed. My notification displays juste fine but I can't log analytics events.
I have tried initializing my firebase app with firebase.initializeApp(config), not knowing if it was initialized in a headless js task but it didn't seem to make a difference.
In-app events log just fine using the Debug View in the firebase console, as well as the notification_receive one automatically
export default async (message: RemoteMessage) => {
const localNotification = new firebase.notifications.Notification()
// setting notification props
const displayNotification = await firebase.notifications().displayNotification(localNotification);
firebase.analytics().logEvent('notification_test', { test: 'test123' }); // this doesn't work
return Promise.resolve(displayNotification);
};
Is there a way to log an event here?
Also, any information on how Headless js works (besides official doc that I've already read of course) would be appreciated.
Thank you!
I have a simple Worklight V6 app using Push on Android. When the app starts up, it does a form based login against the SampleAppRealm. Then it subscribes if necessary:
function checkSubscribed() {
var subscribed = WL.Client.Push.isSubscribed("myPush");
alert("Is subscribed: " + subscribed);
return subscribed;
}
WL.Client.Push.onReadyToSubscribe = function() {
WL.Client.Push.registerEventSourceCallback("myPush", "PushAdapter",
"MyEventSource", function(props, payload) {
alert("Received message: " + props.alert);
});
if (!checkSubscribed()) {
WL.Client.Push.subscribe("myPush", {
onSuccess : function() {
alert("subscription succeeded!");
},
onFailure : function() {
alert("subscription failed!");
}
});
}
};
This all works swell. The app starts up, it logs in, onReadyToSubscribe() fires, it registers the callback, checks the subscription (which is false), and subscribes (which succeeds)
I can see the client subscription in the Worklight console, and if I call the adapter, I get the notification in the app.
If I hit the home button to do something else on the phone, and then return to the app, it still knows that it is subscribed. If I push a message when the app is in the background, I see it in the Android notification area, and see it in the app when I return to it. All good.
The problem is when I am running the app, it is subscribed, and I hit the Android back button. The app closes, but the Admin console still shows a subscribed client, and in fact if I push a message with the app closed, it shows up in the Android notification area. (so far so good)
But when I start the app, it goes through the authentication steps, onReadyToSubscribe() fires, checkSubscribed() gets called and WLClient.Push.isSubscribed() returns false.
If I restart the phone with the app subscribed, when the app restarts, WLClient.Push.isSubscribed() returns false.
How do I get the correct subscription state when an app restarts?
Answering my own question.
It turns out that the problem was that My app was explicitly calling WL.Client.login("SampleAppRealm") when it started. The application descriptor environment setting of a security test on the environment was not configured.
When I add the security test to the application descriptor, isSubscribed() returns the correct answer after an app is restarted. The funny bit is that everything else having to do with Push worked fine with authentication simply triggered by WL.Client.login()
I can't find any documentation around this requirement for the setting in the application descriptor, so it is hard to make out whether this is user error. At the least, the docs could be punched up in this area.