Hi, Guy
I have got a problem relating to checking for notification permissions - and would be grateful for some help.
Basically, I have written some code based about previous post, see
https://www.b4x.com/android/forum/threads/notification-allowed.111064/
which includes a response from Erel on how to check for permissions.
My code is a loop, which:-
1. checks for permission (if not already allowed will display a message explaining why it needs the permission).
2. Then it will setup the notification (hopefully the user will allows notifications)
3. Next it re-checks this permission and, after displaying a message will give the option to go to settings to set the permission directly).
Back to start of loop
The problem is the second check (item 3 above) always returns false on the first pass (even if the user allows the Permission). Second pass is OK
I have tried putting a delay after the App.Register.... instruction - but that does not work
(B4X I am a be fan of B4X but this code is inserted in the "Main" module and I would like to use xui.MsgboxAsync() but for some reason only Msgbox2() works - but that is for another post).
For reference This the object C code to do the permission check!
Hope someone can give me a pointer
Regards (as always)
Dave
I have got a problem relating to checking for notification permissions - and would be grateful for some help.
Basically, I have written some code based about previous post, see
https://www.b4x.com/android/forum/threads/notification-allowed.111064/
which includes a response from Erel on how to check for permissions.
My code is a loop, which:-
1. checks for permission (if not already allowed will display a message explaining why it needs the permission).
2. Then it will setup the notification (hopefully the user will allows notifications)
3. Next it re-checks this permission and, after displaying a message will give the option to go to settings to set the permission directly).
Back to start of loop
The problem is the second check (item 3 above) always returns false on the first pass (even if the user allows the Permission). Second pass is OK
Loop to check permissions - code fragment:
Dim permissionOk As Boolean = False
Do While permissionOk = False
Wait for (CheckNotificationPermission) complete(status As Boolean)
If status = False Then ' Tell user why permissions are required
Dim msg As String = "The App will ask permission to use notifications." & CRLF & _
"These notifications are required for the Centre to communicate with your phone and are only used within the APP." & CRLF & _
"Your notification information is not disclosed to any third parties!" & CRLF & _
"IMPORTANT: You must allow this permission for the App to run correctly."
Msgbox2("Msg", msg, "Notification permission", Array("OK")) 'TODO Why can't we use xui.MsgBoxAsync()?
Wait For Msg_Click (ButtonText As String)
End If
App.RegisterUserNotifications(True, True, True) ' Register notications (ask permission if required)
App.RegisterForRemoteNotifications
' Tried to put Sleep(1000) to slow things down but it don't work!
Wait for (CheckNotificationPermission) complete(status2 As Boolean)
' This is the problem on the fist pass it always returns false!!!!
If status2 = True Then ' Check permissions again and if not allowed warn the user or ask to go to settings.
permissionOk = True
Else
Dim msg As String = "This App will not run correctly without notifications permission." & CRLF & _
"You can go settings and allow Notifications or " & CRLF & _
"remove and re-install, then allow notifications when asked."
Msgbox2("Msg", msg, "Notification permission", Array("OK", "Settings")) 'TODO Why can't we use xui.MsgBoxAsync()?
Wait For Msg_Click (ButtonText As String)
If ButtonText = "Settings" Then
' see https://www.b4x.com/android/forum/threads/open-default-settings-app.51115/#content
If App.OSVersion >= 8 Then
App.OpenURL("app-settings:")
End If
End If
End If
Loop
#End If
I have tried putting a delay after the App.Register.... instruction - but that does not work
(B4X I am a be fan of B4X but this code is inserted in the "Main" module and I would like to use xui.MsgboxAsync() but for some reason only Msgbox2() works - but that is for another post).
For reference This the object C code to do the permission check!
Object C code to check for permissions (from Erel):
' See https://www.b4x.com/android/forum/threads/notification-allowed.111064/
#if OBJC
#import <UserNotifications/UserNotifications.h>
- (void) NotificationStatus {
[[UNUserNotificationCenter currentNotificationCenter] getNotificationSettingsWithCompletionHandler: ^(UNNotificationSettings* settings) {
[self.bi raiseUIEvent:nil event:@"authorization_status:" params:@[@(settings.authorizationStatus)]];
}];
}
Hope someone can give me a pointer
Regards (as always)
Dave