No valid aps-environment string found

I use the NotificationCenter in an iOS app to receive remote notification. With this I have 2 problems.

  1. In Xojo help is described you should call “NotificationCenter.RegisteredForRemoteNotifications” in the App.Opening event. If I do this the app crash with the error “Notifications were not added to this project.”. If I call this later through a button, it functions without any problems. Same in iOS simulator and on a physical device.

  2. If I call “NotificationCenter.RegisterForRemoteNotifications” in the iOS simulator I receive from the error-event of the NotificationCenter the error “No valid aps-environment string found”. On a physical test-device the same code is function.

Any help is appreciated.

Did you enable remote notifications in the app identifier in your Apple Developer account?

and also in the Capabilities section in the iOS build target in Xojo?

Yes, both is enabled.

“NotificationCenter.RegisterForRemoteNotifications” is function if I run the debugging on a physical device (iPhone), but in the iOS simulator I receive the error “No valid aps-environment string found”.

You may not be able to test these in a simulator. Remote notifications require an identifier and there are just some things that require a physical device. This may be one of them.

I don’t believe it was when they were implemented, but things change on Apple’s end. It could also be a Xojo issue though. We would need to use the Run Paused feature and then inspect the entitlements to see.

According to the Xojo documentation section “Testing remote notifications” this should be possible in the iOS simulator:

https://documentation.xojo.com/api/mobile/mobilenotifications.html

But how you say, perhaps something changed in time, then the documentation should be updated.

You have also an idea about the other problem if I call in the App.Opening event the “RegisteredForRemoteNotifications”? I think is almost necessarily that this function, I not see how I should register else for the push notifications.

Just make sure you are calling the right thing. There is a method called RegisterForRemoteNotifications which tells the device that you would like to get a device ID, and there is a property named RegisteredForRemoteNotifications which returns True if the method above was successful.

I call NotificationCenter.RegisterForRemoteNotifications in the App.Opening event and receive every time the error “Notifications were not added to this project”.

If I call the exact same method later over a button on a screen it functions. Is this an error in Xojo?

It could be that Xojo or Apple changed something and changed how early the Opening event fires, but I know this used to work.

Yeah I can see that here in an empty project. Another case where Xojo’s broke embedding the entitlements when building a simulator project :disappointed:

I’ve filed a bug report about this because I’m also not getting a token when registering in Screen1.Opening

Hello,

In you app properties:
create a property “token” as string

in your app.opening, put it a the very beginning of the event

// Do this to clear any categories that already exist
NotificationCenter.RemoveAllResponseCategories

Var cat As New NotificationResponseCategory("MY_CATEGORY")

cat.actions.Add New NotificationResponseButton("Yes", "YES")
cat.actions.add New NotificationResponseButton("No", "NO")

Var txtfield As New NotificationResponseTextField("Instant Reply", "Type your reply...", "Send", "REPLY")

// Add it to the category
cat.actions.add txtfield
NotificationCenter.AddResponseCategory(cat)
NotificationCenter.RequestAuthorization(MobileNotifications.AuthorizationOptions.Alert, _
MobileNotifications.AuthorizationOptions.Badge, MobileNotifications.AuthorizationOptions.Sound)

In a your primary screen, add a timer (Single/ 2000 or more)

try
  if not NotificationCenter.RegisteredForRemoteNotifications then
    NotificationCenter.RegisterForRemoteNotifications
  end if
catch error as UnsupportedOperationException
  System.DebugLog("Error register APNS")
  return
end try

in the NotificationCenter, events "RemoteRegistrationSucceeded

dim mb as MemoryBlock = token.LeftBytes(token.Length)
app.token = Xojo.Core.TextEncoding.UTF8.ConvertDataToText(mb)

System.DebugLog("Token APNS :"+app.token)

i use this method inn my apps, without timer it’s fail. With it’s work.

A simpler solution:

Make a new method in the App class called StartNotifications:

Public Sub StartNotifications()
  NotificationCenter.RequestAuthorization(MobileNotifications.AuthorizationOptions.Alert)
  NotificationCenter.RegisterForRemoteNotifications
End Sub

In App.Opening, make the last line:

timer.CallLater(10, AddressOf StartNotifications)

Thank you very much for the help, I will implement it that way.