iOS中的expo背景推送通知

时间:2019-04-21 14:38:44

标签: javascript ios react-native apple-push-notifications expo

起初,我担心博览会有一个自定义的通知系统(如果您知道这样的广告,我是否想过“ coulda可能有p8”),哈哈。最后,通过expo的推送通知服务直接和轻松地发送消息给我留下了深刻的印象。我遇到的唯一问题是开始意识到此永远不会在模拟器上运行。否则,它确实很漂亮,甚至比p8还容易。

但是,当我尝试发送后台通知时,我注意到了另一种行为方式。在那种情况下,似乎什么也没发生。

我的平台是iOS,我的App组件具有一个扩展名,它绑定到this,然后仅触发一次:

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)
}

在其他地方,我共享该令牌,以便向其发送消息。我注意到服务器发送后台消息时它无法正常工作,尽管它确实从POST中注册为成功(没有打开应用程序,也没有添加Alert弹出窗口……即使该应用程序位于前景,没有弹出窗口)。因此,我去了PN tool,果然是我工作的第一个“ hello world”。立即,它也起作用。

但是如果我像配置自己的消息一样配置它,并提供JSON数据,类别和内容-没有其他,则不会发生任何事情。不在前台,不在后台。关闭时不行。 :皱眉:

0 个答案:

没有答案