laravel 使用 onesignal 向特定用户发送通知

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42709827/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 15:31:48  来源:igfitidea点击:

Send Notification to specific user using onesignal

phplaravelnotificationsonesignalweb-push

提问by Akshay Vaghasiya

I'm using onesignalfor first time in my project. I have to send notification to specific users (wherever user is logged in either on chrome or android device) on some events like whatsapp sends notification on mobile as well as website too and only if user is logged in. I have successfully sent notification to all users. After some R&D on OneSignal, I come to know that I have to send notifications to specific playerId for this purpose. But here are some my questions/doubts.

我第一次在我的项目中使用onesignal。我必须在某些事件上向特定用户发送通知(无论用户在 chrome 还是 android 设备上登录),例如 whatsapp 也会在移动设备和网站上发送通知,并且仅当用户登录时。我已成功向所有人发送通知用户。在对 OneSignal 进行了一些研发之后,我知道我必须为此目的向特定的 playerId 发送通知。但这里有一些我的问题/疑虑。

  • I think player Id is device id. If I'm right, what if current user logged out and another user gets logged in on same device. How can I come to know that I have not to send notification on this player Id as user is changed.
  • Will be there multiple playerIds for single user? One for chrome, one for iOS device, one for Android etc?
  • How I will get playerId of user to store in my database. Because I have to use server rest APIto send notifications.
  • 我认为玩家 ID 是设备 ID。如果我是对的,如果当前用户注销而另一个用户在同一设备上登录怎么办。我怎么知道我不必在用户更改时发送有关此玩家 ID 的通知。
  • 单个用户会有多个 playerIds 吗?一种用于 chrome,一种用于 iOS 设备,一种用于 Android 等?
  • 我将如何让用户的 playerId 存储在我的数据库中。因为我必须使用服务器休息 API来发送通知。

If anyone can explain the points and also describe work flow to store playerid (when and how) in my database, it will be appreciated.

如果有人可以解释这些要点并描述在我的数据库中存储玩家 ID(何时以及如何)的工作流程,我们将不胜感激。

采纳答案by Kishan

Just store playerIds into database with login activity. when they logout just fire a query related to remove playerIds from database.

只需将 playerIds 存储到具有登录活动的数据库中。当他们注销时,只需触发一个与从数据库中删除 playerIds 相关的查询。

回答by Dylan Auty

I developed an android app not too long ago which used OneSignal for it's notifications. This was developed in C# however may be useful to you.

不久前我开发了一个 android 应用程序,它使用 OneSignal 来发送通知。这是用 C# 开发的,但可能对您有用。

I hope these answers help:

我希望这些答案有帮助:

-The Player ID is generated by the OneSignal class, and as such need to be retrieved using callback function, such as the code seen below:

- 播放器 ID 由 OneSignal 类生成,因此需要使用回调函数检索,如下所示:

void CheckPushRegistration(){
    OneSignal.GetIdsAvailable(IdsAvailable); //Passes the 'callback'

}

private void IdsAvailable(string userID, string pushToken) {
    player_id = userID;
    player_token = pushToken;

    //Make a request to my server to store this information along with device id
    //For easy reference
}

Note: In the code above, I call the function 'CheckPushRegistration' right at the start of the app run. Once it is run it will send the information to the 'IdsAvailable' function. After this I make a quick web request to my server to store the unique player ID, device ID, and token.

注意:在上面的代码中,我在应用程序运行开始时调用了函数“CheckPushRegistration”。运行后,它会将信息发送到“IdsAvailable”函数。在此之后,我向我的服务器发出快速 Web 请求以存储唯一的玩家 ID、设备 ID 和令牌。

-The Player ID, at least in android is based on the devices linked Google Play account if I am not mistaken. Meaning if a user switches accounts it will count/act as a different Player ID (However I could be wrong here)

- 如果我没记错的话,播放器 ID,至少在 android 中是基于与 Google Play 帐户关联的设备。意思是如果用户切换帐户,它将算作/充当不同的玩家 ID(但是我在这里可能是错的)

I assume OneSignal has some way of determining the Player ID or User ID on browsers/other devices. However it is safe to assume they will always be unique and do not carry through based on the user account.

我假设 OneSignal 有某种方法可以确定浏览器/其他设备上的玩家 ID 或用户 ID。但是,可以安全地假设它们将始终是唯一的,并且不会基于用户帐户进行。

-As mentioned earlier you store it into your database by making a simple web request to your REST API, store only the Device ID, Player ID, and Token information. Perhaps add a check to see if the device ID is not already in the system. I would recommend using the Device ID to identify a user.

- 如前所述,您通过向 REST API 发出简单的 Web 请求将其存储到数据库中,仅存储设备 ID、玩家 ID 和令牌信息。也许添加一个检查以查看系统中是否还没有设备 ID。我建议使用设备 ID 来识别用户。

You also asked how you would know if a notification was sent correctly, or if it was safe to send. Unfortunately the OneSignal API will only be able to report back on a notification status.

您还询问了如何知道通知是否正确发送,或者发送是否安全。不幸的是,OneSignal API 只能报告通知状态。

When you do send the notification, you will receive a notification ID back from the server. You can use this at a later stage to checked if a notification was delivered, if it was viewed, and if it was tapped.

当您发送通知时,您将从服务器收到一个通知 ID。您可以在稍后阶段使用它来检查通知是否已发送、是否已查看以及是否已被点击。

I hope this helps to shed some light on the OneSignal system?

我希望这有助于对 OneSignal 系统有所了解?

回答by prashant kute

Player Id is not a device Id,it is a UUID (Unique Universal Identifier) that OneSignal gives

播放器 ID 不是设备 ID,而是 OneSignal 提供的 UUID(唯一通用标识符)

Managing Multiple Users On One Device To manage multiple users that share a device, you will need to detect when User A is on the device and only send them their notifications when they are on the device, otherwise User B will get them.

在一台设备上管理多个用户 要管理共享一个设备的多个用户,您需要检测用户 A 何时在设备上,并且只有在他们在设备上时才向他们发送通知,否则用户 B 会收到通知。

Method A: Tagging One way to handle this is to tag the user when they sign in to the app, then you can target them by the tag and they will get the notification if the tag is set. Once they log out of the app, you can remove the tag.

方法 A:标记 处理此问题的一种方法是在用户登录应用程序时对其进行标记,然后您可以通过标记定位他们,如果设置了标记,他们将收到通知。一旦他们退出应用程序,您就可以删除标签。

So if User B is using the device, then they will get their own tag and will not get User A's notification. So the users will only get the notifications when they are logged into the app.

因此,如果用户 B 正在使用该设备,那么他们将获得自己的标签,而不会收到用户 A 的通知。因此,用户只有在登录应用程序时才会收到通知。

Method B: CRM or Database The other option is to store on your server the Player IDs of the devices the user is currently logged in with, and then use our API to send notifications using the "include_player_ids" API targeting parameter. for more detail check this One signal player id

方法 B:CRM 或数据库 另一种选择是将用户当前登录设备的玩家 ID 存储在您的服务器上,然后使用我们的 API 使用“include_player_ids”API 定位参数发送通知。有关更多详细信息,请检查此 One signal player id