使用OneSignal / Firebase从iOS设备发送推送通知 [英] Sending push notifications from iOS device using OneSignal/Firebase

查看:1382
本文介绍了使用OneSignal / Firebase从iOS设备发送推送通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个应用程序,并试图解决在不同用户执行特定操作时如何通知特定用户。我试图避免建立自己的后端服务器,因为我相信我想要做的事情可以通过OneSignal / Firebase完成。



我没有试过它还没有,但我想每个用户我可以得到他们自己的OneSignal player_id(这基本上是OneSignal device_id),将其上传到我的firebase数据库,然后当我想发送推送通知时,我只需从用户的firebase然后使用它创建一个通知与OneSignals 发送到特定设备,其中使用player_ids不需要API认证密钥。



虽然查看文档后我不确定用户如何找到(所以他们可以上传到firebase)他们的player_id,所以如果任何人都可以指向我的方向,这将不胜感激。

通过其他问题的阅读,这似乎是工作。我只是想看看是否有任何类似的潜在问题,或者有什么更安全或更简单的方法来解决这个问题。

是的,这是可能的,您需要做的是:
$ b $ ul

  • 创建一个OneSignal和Firebase帐户API-钥匙


  • 添加一个信号(只是一个例子)

      OneSignal.initWithLaunchOptions(launchOptions,appId:your app id,handleNotificationReceived:{(通知)in 

    },handleNotificationAction:{(结果)in
    let payload:OSNotificationPayload?= result ?.notification.payload
    print(payload ??)

    },settings:[kOSSettingsKeyAutoPrompt:false,kOSSettingsKeyInFocusDisplayOption:OSNotificationDisplayType.notification.rawValue])
  • 将push token / playerId保存到firebase(例如在users.id.onesignal中)

      OneSignal.registerFo 
    打印(UserId:\(userId!))
    OneSignal.idsAvailable({(_ userId,_ pushToken)如果pushToken!= nil {$ b $ (pushToken:\(pushToken!))
    FIRDatabase.database()。reference()。child(users)。child(User.sharedInstance.uid).child(onesignal) .setValue([userId!:pushToken!])

    }
    })


  • 当每个用户都有一个pushToken的时候,你可以发送一个通知给这个playerId

      FIRDatabase ()的快照($快照$ b $如果snapshot.exists()。).database()。reference()。child(users)。child(friendId).child(onesignal)。 {

    (playerId,_)在snapshot.value中为! Dictionary< String,String> {

    OneSignal.postNotification([headings:[en:title],contents:[en:message],include_player_ids:[playerId]])














    这并不是完整的实施方式,而是如何去做,但它会给你正确的方式。


    I'm developing an app and am trying to work out how I can notify a specific user when a different user performs a specific action. I'm trying to avoid setting up my own backend server, as I believe that what I want to do can be accomplished by OneSignal/Firebase.

    I haven't tried it yet, but I figure with each user I could get their own OneSignal player_id (which is basically the OneSignal device_id), upload that to my firebase database, then when I want to send a push notification I simply pull the user's player_id from firebase and then use it to create a notification with OneSignals send to specific device, where using player_ids does not require the API Auth Key.

    Though after looking through the documentation I'm not sure how a user can find (so they can then upload to firebase) their player_id, so if anyone could point me in that direction it would be appreciated.

    Reading through other questions, it seems like this would work. I just wanted to see if there were any like potential issues with this method or if there were any safer or easier ways to go about it.

    解决方案

    Yes this is possible, what you need to do is:

    • Create an OneSignal and Firebase Account for API-Keys

    • Add One Signal (just an example)

      OneSignal.initWithLaunchOptions(launchOptions, appId: "your app id", handleNotificationReceived: { (notification) in
      
          }, handleNotificationAction: { (result) in
              let payload: OSNotificationPayload? = result?.notification.payload
              print(payload ?? "")
      
          }, settings: [kOSSettingsKeyAutoPrompt : false, kOSSettingsKeyInFocusDisplayOption: OSNotificationDisplayType.notification.rawValue])
      

    • save the push token/playerId to firebase (in users.id.onesignal for example)

      OneSignal.registerForPushNotifications()
      OneSignal.idsAvailable({(_ userId, _ pushToken) in
      print("UserId:\(userId!)")
      if pushToken != nil {
          print("pushToken:\(pushToken!)")
          FIRDatabase.database().reference().child("users").child(User.sharedInstance.uid).child("onesignal").setValue([userId!: pushToken!])
      
      }
      })
      

    • When you have for every user an pushToken, you can send a notification to that playerId

      FIRDatabase.database().reference().child("users").child(friendId).child("onesignal").observeSingleEvent(of: .value, with: {snapshot in
          if snapshot.exists() {
      
              for (playerId, _) in snapshot.value as! Dictionary<String, String> {
      
                  OneSignal.postNotification(["headings": ["en": title], "contents": ["en": message], "include_player_ids": [playerId]])
              }
          }
      })
      

    This is not the full implementation and how to do it, but it will give you the right way I think.

    这篇关于使用OneSignal / Firebase从iOS设备发送推送通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆