iOS Tutorial Send Push Notification with OneSignal

Hi there,


In this tutorial I use onesignal rest api and send device token to onesignal and store it

1. Register in onesignal.com and create an application

2. Export .p12 file from certificate using keychain in mac and upload in OneSignal panel -> App Settings -> push -> Apple Push Certificates

3. Import your .p12 files in setting

4. Import Onesignal class to project (attached)

5. Add this codes to progress_globals in

B4X:
Dim signal as onesginal
  Dim applicationId As String = "Application_ID"
  Dim appversion as String = "1.1"

6. Add this codes to application_start

B4X:
App.RegisterUserNotifications(True, True, True)
App.RegisterForRemoteNotifications
CheckForPushMessage

7. And add subs to Main


B4X:
Private Sub Application_Start (Nav As NavigationController)
   NavControl = Nav
   Page1.Initialize("Page1")
   Page1.Title = "Page 1"
   Page1.RootPanel.Color = Colors.White
   NavControl.ShowPage(Page1)

   App.RegisterUserNotifications(True, True, True)
   App.RegisterForRemoteNotifications
   CheckForPushMessage
End Sub

Sub CheckForPushMessage
   If App.LaunchOptions.IsInitialized And _
     App.LaunchOptions.ContainsKey("UIApplicationLaunchOptionsRemoteNotificationKey") Then
     Dim data As Object = App.LaunchOptions.Get("UIApplicationLaunchOptionsRemoteNotificationKey")
     Dim no As NativeObject = App
     no.GetField("delegate").RunMethod("application:didReceiveRemoteNotification:", _
       Array(App, data))
   End If
End Sub

Private Sub Application_PushToken (Success As Boolean, Token() As Byte)
   If Success Then
     Dim bc As ByteConverter
    signal.Initialize("app_id="&applicationId&"&device_type=0&device_os="&App.osVersion&"&device_model="&GetDeviceModel&"&identifier="&bc.HexFromBytes(Token)&"&game_version"&appversion&"&test_type=1")

   Else
     Log("Error getting token: " & LastException)
   End If
End Sub

Sub GetDeviceModel As String
    ' get the device model
    Dim device As NativeObject
    device = device.Initialize("UIDevice").RunMethod("currentDevice", Null)
    Dim name As Object = device.GetField("model").AsString
    Return name
End Sub


Private Sub Application_RemoteNotification (Message As Map)
   Log("Remote notification: " & Message)
   Dim m As Map = Message.Get("aps")
   Log(m)
   Log(m.Get("alert"))
   Msgbox("remote: " & Message, "")
End Sub
Note : In distribution version delete test_type=1 from initialize

8. Now go to push in onesginal and send a push notification
 

Attachments

  • onesignal.bas
    991 bytes · Views: 686

Alberto Iglesias

Well-Known Member
Licensed User
Longtime User
Hello cloner7801,

I create all process in OneSignal, my app is sending the token through API you provide, and I can see in "All Users section" this device, like this:

but when I test some notification, I can see 1 device sent,but this message never arrived!

token2.png



My certificate is ok, because I test with another push tester, this one: https://pushtry.com/ is working fine.....

The code to send token to OneSignal, I using your example and I think is right.

B4X:
Private Sub Application_PushToken (Success As Boolean, Token() As Byte)
   
    Dim bc As ByteConverter
    Dim strToken As String = bc.HexFromBytes(Token)
    Initialize("app_id=" & OneSignal_ID & "&device_type=0&device_os=" & App.osVersion & "&device_model=" & GetDeviceModel & "&game_version=" & appversion & "&identifier=" & strToken)
       
End Sub


do I missing something?
 

cloner7801

Active Member
Licensed User
Longtime User
Hello cloner7801,

I create all process in OneSignal, my app is sending the token through API you provide, and I can see in "All Users section" this device, like this:

but when I test some notification, I can see 1 device sent,but this message never arrived!

token2.png



My certificate is ok, because I test with another push tester, this one: https://pushtry.com/ is working fine.....

The code to send token to OneSignal, I using your example and I think is right.

B4X:
Private Sub Application_PushToken (Success As Boolean, Token() As Byte)
  
    Dim bc As ByteConverter
    Dim strToken As String = bc.HexFromBytes(Token)
    Initialize("app_id=" & OneSignal_ID & "&device_type=0&device_os=" & App.osVersion & "&device_model=" & GetDeviceModel & "&game_version=" & appversion & "&identifier=" & strToken)
      
End Sub


do I missing something?
You should use sandbox (development) p12 in onesignall
 

Alberto Iglesias

Well-Known Member
Licensed User
Longtime User
I found my mistake....... not with OneSignal.

I choose OneSignal because Firebase Notifications is not working, but now I found why:

The Logs don't say nothing about.... The Log and Event says; Firebase is connected, etc... But when send any messsage, don't arrive nothing.

I just double check on all processes, and I found: Some Libraries are corrupted, like FirebaseMessaginf.framework. I Just updated, and now is working...

Crazy, but is true!


Thank you so much
 
Top