expo background push notfication in iOS

27
April 22, 2019, at 03:00 AM

At first, I was worried that expo had a custom notification system (had me thinking "coulda had a p8", if you know the commercial like that, haha). In the end, I was impressed with how direct and easy sending messages with expo's push notification service is. The only problem I ran into was in coming to realize that this will never work on a simulator. Otherwise, it's pretty spiffy really, even easier than a p8.

But I'm noticing a different pattern of behavior when I try to send a background notification. In that instance, nothing seems to be happening.

My platform is iOS, and my App component has an extension that it binds to this and then fires off only once:

extensionAPNs.js
import { Alert } from 'react-native'
import { Permissions, Notifications } from 'expo'
import AsyncStorage from '@callstack/async-storage'
import { handleNotification } from '@src/lib/APNs'
export async function registerForPushNotifications () {
  const { status } = await Permissions.getAsync(Permissions.NOTIFICATIONS)
  if (status !== 'granted') {
    const { status } = await Permissions.askAsync(Permissions.NOTIFICATIONS)
    if (status !== 'granted') {
      Alert.alert('received no permission to listen to APNs')
      return
    }
  }
  const expoToken = await Notifications.getExpoPushTokenAsync()
  Alert.alert('recieved permission to listen to APNs with token ' + expoToken)
  this.subscription = Notifications.addListener(handleNotification)
  this.setState({ expoToken })
  AsyncStorage.setItem('expoToken', expoToken)
}
import { Alert } from 'react-native'
export async function handleNotification () {
  Alert.alert(JSON.stringify(arguments))
  console.log('arguments', arguments)
}

Elsewhere, I share that token so I can message it. I noticed it wasn't working when my server sent its background message, although it does register as a success from the POST (didn't open the app, didn't add the Alert popup... even if the app was in the foreground, no popup). So I went to the PN tool and sure enough, the first "hello world" that I did work. Immediately, it works, too.

But if I configure it like my own message, with JSON data, category, and content available -- and nothing else, nothing happens. Not in the foreground, not in the background. Not when closed. :frowning:

READ ALSO
Post back on checkbox change

Post back on checkbox change

I want to post back whenever checkbox is changed for a Tree view, I searched and found answers but it isn't workingWhen i debug it never gets to the event function

50
Saving nested objects(arrays) with mongoose, result empty objects at mongodb

Saving nested objects(arrays) with mongoose, result empty objects at mongodb

The problem is so basic: I want to save nested objects with mongoose in mongodb, but when I save them, nested objects are empty

60
Convert Array from JSON File into a Javascript Array does not work

Convert Array from JSON File into a Javascript Array does not work

even though I read alot of related articles about this topic on stackoverflow, I cant figure out what im doing wrong here

23
Accessing parent callback function in nested child components

Accessing parent callback function in nested child components

I'm trying to create a globally accessible modal by implementing this modal in a top-level component in React NativeTo achieve this, I try to extend SwitchNavigator (based on this link) with a callback function and make this accessible through navigation...

18