Notifications
8 min
the appstorys sdk ships its own click handler, notification rendering, and outreach event tracking react native has no equivalent of a firebasemessagingservice that a library can register for you — fcm messages are delivered to javascript, and only your app can register the background handler so there are two things to do hand the sdk your fcm messages, and forward the firebase cloud messaging (fcm) token to appstorys call appstorys setfirebasetoken(token) whenever you have an fcm token const token = await gettoken(getmessaging(getapp())); appstorys setfirebasetoken(token); this registers the device with appstorys outreach backend so push campaigns can be delivered forwarding messages to the sdk background and killed state pushes must be wired up at module scope in index js, outside the react tree — inside a component they never run in that state const messaging = getmessaging(getapp()); setbackgroundmessagehandler(messaging, async (remotemessage) => { const handledbyappstorys = await appstorysmessaging handlemessage(remotemessage); if (handledbyappstorys) return; // any other provider / your own default handling }); // notification taps while backgrounded / killed appstorysmessaging registerbackgroundevents(); foreground pushes, from anywhere in your app const unsubscribe = onmessage(messaging, async (remotemessage) => { const handledbyappstorys = await appstorysmessaging handlemessage(remotemessage); if (handledbyappstorys) return; // your own handling }); handlemessage() returns true only for appstorys outreach pushes, so your own handling falls through untouched always await it — resolving the promise is what keeps the headless js task alive until the "viewed" event lands, the role goasync() played on android foreground taps and the tap that cold starts the app are handled by appstorys initialize() deep links are routed through the navigatetoscreen callback you pass to it, so that callback must be able to move your app to an arbitrary screen name from a cold start when to call it on every new token from firebase fcm rotates tokens periodically — after app reinstall, data clear, long inactivity, or at firebase's discretion subscribe to ontokenrefresh and forward it there const unsubscribe = ontokenrefresh(messaging, (token) => { appstorysmessaging handlenewtoken(token); // your own backend, other sdks }); use handlenewtoken on the refresh path rather than setfirebasetoken a refresh can land before initialize() has finished, and handlenewtoken degrades to caching the token locally so the next initialize() picks it up if you use another push provider, send its device token to appstorys as well managing push opt in/opt out if your app has its own notification preference toggle (separate from the os permission), use appstorys subscribenotifications(); appstorys unsubscribenotifications(); call unsubscribenotifications() when the user turns your in app push toggle off, and subscribenotifications() when they turn it back on this only affects whether appstorys sends campaigns to the user server side — it doesn't remove the fcm token os level permission changes (user denies/allows notifications from system settings) are already detected and synced automatically — you don't need to call this yourself for that case requires the sdk to already be initialized and userid to be set after your own permission prompt resolves, call appstorys onnotificationpermissionresult() so the new state reaches the backend immediately instead of waiting for the next foreground enabling heads up (pop up) notifications by default, appstorys notifications may appear silently in the notification tray rather than popping up as a heads up banner to make them pop up, create the notification channel with high importance before the sdk does — it creates the channel at default importance during initialize(), so declare it just above that call if (platform os === 'android') { await notifee createchannel({ id 'appstorys outreach', name 'outreach', importance androidimportance high, }); } android freezes a channel's importance once created, so this must land before the first push on a given install notification icon android 8+ renders a white square unless the status bar icon is a monochrome (alpha only) drawable either add one named ic notification to your app, or point the sdk at yours before the first push arrives appstorysmessaging configure({ smallicon 'ic notification', color '#ff6b00' });