iOS Question Check for notification permissions

davemorris

Active Member
Licensed User
Longtime User
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

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
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
B4X:
   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."
Please:
B4X:
   Dim msg As String = $"The App will ask permission to use notifications.
These notifications are required for the Centre to communicate with your phone and are only used within the APP.
Your notification information is not disclosed to any third parties!
IMPORTANT: You must allow this permission for the App to run correctly."$

Where is the code of: CheckNotificationPermission ?
 
Upvote 0

davemorris

Active Member
Licensed User
Longtime User
Hi, Erel - thanks the the quick response

Very sorry I missed that bit of code out (I was trying to avoid complicating the issue with too much code). Please the requested code below:
Code to check for permissions.:
' Check if permission for notifications
' See https://www.b4x.com/android/forum/threads/notification-allowed.111064/
Sub CheckNotificationPermission As ResumableSub
    Dim permissionOk As Boolean = False
    Dim no As NativeObject = Me
    no.RunMethod("NotificationStatus", Null)
    Wait For Authorization_Status(status As Int) '=0 - not determined, =1 - denied, =2 - authroized and =3 - provisional (authorized)
    Log(status)
    If status <> 0 And status <> 1 Then
        permissionOk = True
    End If
    Return permissionOk
End Sub

Hope that helps

Regards
Dave
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
I've ran this code:
B4X:
Private Sub Application_Start (Nav As NavigationController)
    analytics.Initialize
    NavControl = Nav
    Page1.Initialize("Page1")
    Page1.Title = "Page 1"
    Page1.RootPanel.Color = Colors.White
    NavControl.ShowPage(Page1)
    Wait For (CheckNotificationPermission) Complete (Status As Boolean)
    Log(Status)
    App.RegisterUserNotifications(True, True, True)
    App.RegisterForRemoteNotifications
    fm.Initialize("fm")
End Sub
The output of line 9 was True.
 
Upvote 0

davemorris

Active Member
Licensed User
Longtime User
Hi, Erel
I would agree that line 9 is true if you have previously allowed the permissions, however as I am testing after the first installation, I am sure the line would return false.
Then if you allow the permissions (i.e. lines 10 or 11 - not sure which) - However if you insert code to check the permission again (see below modifications to your code - currently untested) I believe it would return false.
Please note: I have left in line 13 - no sure how this is relevant to the problem.
Modification to Erel's code see lines 13 and 14:
Private Sub Application_Start (Nav As NavigationController)
    'analytics.Initialize
    'NavControl = Nav
    'Page1.Initialize("Page1")
    'Page1.Title = "Page 1"
    'Page1.RootPanel.Color = Colors.White
   ' NavControl.ShowPage(Page1)
    Wait For (CheckNotificationPermission) Complete (Status As Boolean)
    Log(Status)
    App.RegisterUserNotifications(True, True, True)
    App.RegisterForRemoteNotifications
    fm.Initialize("fm") '<--- Not sure if is relevant
    Wait For (CheckNotificationPermission) Complete (Status As Boolean) '<--- Inserted lines
    Log(Status) ' I believe this will always return false on the first pass after installation and the notifications are allowed.
End Sub
 
Upvote 0

davemorris

Active Member
Licensed User
Longtime User
Hi Erel, Sorry somehow,
I sent my replay too early (a bit of figure trouble?).
I have removed some lines
Lines 2 and 3 are done earlier in my code.
and lines 4 - 7 are done later, and 12 throws an error.

However, I did run the code and put a break at line 14 - and (I now think problem relates to an asynchronous problem). It stopped at line 14 (via the break point as expected) but still showed the "Request to send Notifications" dialog box.

Hope this helps (I did think about editing the previous post but I thought that could complicate matters if someone had already opened it).
Regards
Dave
 
Upvote 0

davemorris

Active Member
Licensed User
Longtime User
Hi Erel
Thanks for your help, including the extra code works as expected- I can now check if the permission is allowed and allow the user to set permission without upsetting the work flow.

However, I have a question about the initial check (see my opening post)
"1. checks for permission (if not already allowed will display a message explaining why it needs the permission). "

The question is: Elsewhere in my App it checks for location and uses the #PlistExtra attribute to give the reason for requiring the permission, which is displayed within the "Request for permission" dialog. Is a similar process available for notifications? (As the method I use, a msgbox , is not very satisfactory).
Location attribute - include in my main module.:
    #PlistExtra:<key>NSLocationWhenInUseUsageDescription</key><string>Used to automatically find your nearest centre.</string>
    #PlistExtra:<key>NSLocationUsageDescription</key><string>Used to automatically find your nearest centre.</string>
Sorry if this takes the post off topic but it does save a lot of explaining. Also I intend to complete this post with an example of the working code and I would not like to include an unnecessary msgbox.

Kind regards
Dave
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…