iOS Question [resolved] Firebase App compiles, but no messages arrive

stingrae

Member
Licensed User
Longtime User
Hi all,

I've got the same problem with an App I'm developing, and also one which I did specifically to try and resolve this issue I've got. In short, I can't get Firebase to get a TokenID or receive a message sent to a topic.

I get no errors when compiling and the App loads on the test devices. I can get the FCMConnected Sub to fire and fm.SubscribeToTopic("ios_general") doesn't return any errors.

The problem is I just can't seem to get a message to come through. (My Android version is working perfectly)

Only hint that I can see something is wrong, is that this code always returns a blank string:

B4X:
Private Sub GetToken As String
    Dim no As NativeObject
    Dim token As NativeObject = no.Initialize("FIRInstanceID").RunMethod("instanceID", Null).RunMethod("token", Null)
    If token.IsInitialized Then Return token.AsString Else Return ""
End Sub

And I saw on another thread that this could mean that there is something wrong in the configuration and this is why I created a 2nd app and went through Erel's tutorials again. But it is the same result.

Is there any further logging I can get that can tell me where I've wrong? I've gone through the tutorials (and searched) for many hours but I can't seem to find the problem.

Thanks in advance.
 

stingrae

Member
Licensed User
Longtime User
Hi @Erel , thanks for the prompt response.

I'm now getting this error when I add a reference to ByteConverter (I take it out and it compiles ok). Is this the correct reference to add?
 

Attachments

  • byteconverter.PNG
    byteconverter.PNG
    100.9 KB · Views: 297
Upvote 0

stingrae

Member
Licensed User
Longtime User
Oh, sorry. This is the error:

CompileC build/B4iProject.build/Release-iphoneos/B4iProject.build/Objects-normal/arm64/b4i_main.o B4iProject/b4i_main.m normal arm64 objective-c com.apple.compilers.llvm.clang.1_0.compiler
cd /Users/administrator/Documents/UploadedProjects/<user id>
export LANG=en_US.US-ASCII
export PATH="/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/usr/bin:/Applications/Xcode.app/Contents/Developer/usr/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin"
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang -x objective-c -arch arm64 -fmessage-length=0 -fdiagnostics-show-note-include-stack -fmacro-backtrace-limit=0 -std=gnu99 -fobjc-arc -fmodules -gmodules -fmodules-prune-interval=86400 -fmodules-prune-after=345600 -fbuild-session-file=/var/folders/35/nx43fs8567l9z_wvvpc35snh0000gn/C/org.llvm.clang/ModuleCache/Session.modulevalidation -fmodules-validate-once-per-build-session -Wnon-modular-include-in-framework-module -Werror=non-modular-include-in-framework-module -Wno-trigraphs -fpascal-strings -O0 -Wno-missing-field-initializers -Wno-missing-prototypes -Werror=return-type -Wno-implicit-atomic-properties -Werror=deprecated-objc-isa-usage -Werror=objc-root-class -Wno-arc-repeated-use-of-weak -Wduplicate-method-match -Wno-missing-braces -Wparentheses -Wswitch -Wunused-function -Wno-unused-label -Wno-unused-parameter -Wunused-variable -Wunused-value -Wempty-body -Wuninitialized -Wconditional-uninitialized -Wno-unknown-pragmas -Wno-shadow -Wno-four-char-constants -Wno-conversion -Wconstant-conversion -Wint-conversion -Wbool-conversion -Wenum-conversion -Wno-float-conversion -Wno-non-literal-null-conversion -Wno-objc-literal-conversion -Wshorten-64-to-32 -Wpointer-sign -Wno-newline-eof -Wno-selector -Wno-strict-selector-match -Wundeclared-selector -Wno-deprecated-implementations -DNS_BLOCK_ASSERTIONS=1 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS11.2.sdk -fstrict-aliasing -Wprotocol -Wdeprecated-declarations -miphoneos-version-min=7.0 -fvisibility=hidden -Wno-sign-conversion -Wno-infinite-recursion -Wno-comma -Wno-block-capture-autoreleasing -Wno-strict-prototypes -iquote /Users/administrator/Documents/UploadedProjects/<user id>/build/B4iProject.build/Release-iphoneos/B4iProject.build/Stingrae\ FBiOS-generated-files.hmap -I/Users/administrator/Documents/UploadedProjects/<user id>/build/B4iProject.build/Release-iphoneos/B4iProject.build/Stingrae\ FBiOS-own-target-headers.hmap -I/Users/administrator/Documents/UploadedProjects/<user id>/build/B4iProject.build/Release-iphoneos/B4iProject.build/Stingrae\ FBiOS-all-target-headers.hmap -iquote /Users/administrator/Documents/UploadedProjects/<user id>/build/B4iProject.build/Release-iphoneos/B4iProject.build/Stingrae\ FBiOS-project-headers.hmap -I/Users/administrator/Documents/UploadedProjects/<user id>/Payload/include -I/Users/administrator/Documents/UploadedProjects/<user id>/build/B4iProject.build/Release-iphoneos/B4iProject.build/DerivedSources/arm64 -I/Users/administrator/Documents/UploadedProjects/<user id>/build/B4iProject.build/Release-iphoneos/B4iProject.build/DerivedSources -F/Users/administrator/Documents/UploadedProjects/<user id>/Payload -F../../Libs -MMD -MT dependencies -MF /Users/administrator/Documents/UploadedProjects/<user id>/build/B4iProject.build/Release-iphoneos/B4iProject.build/Objects-normal/arm64/b4i_main.d --serialize-diagnostics /Users/administrator/Documents/UploadedProjects/<user id>/build/B4iProject.build/Release-iphoneos/B4iProject.build/Objects-normal/arm64/b4i_main.dia -c /Users/administrator/Documents/UploadedProjects/<user id>/B4iProject/b4i_main.m -o /Users/administrator/Documents/UploadedProjects/<user id>/build/B4iProject.build/Release-iphoneos/B4iProject.build/Objects-normal/arm64/b4i_main.o
In file included from /Users/administrator/Documents/UploadedProjects/<user id>/B4iProject/b4i_main.m:2:
/Users/administrator/Documents/UploadedProjects/<user id>/B4iProject/b4i_main.h:5:9: fatal error: 'ByteConverter.h' file not found
#import "ByteConverter.h"
^~~~~~~~~~~~~~~~~
1 error generated.

Error: ** BUILD FAILED **
The following build commands failed:
CompileC build/B4iProject.build/Release-iphoneos/B4iProject.build/Objects-normal/arm64/b4i_main.o B4iProject/b4i_main.m normal arm64 objective-c com.apple.compilers.llvm.clang.1_0.compiler
(1 failure)
 
Upvote 0

stingrae

Member
Licensed User
Longtime User
Thanks @Erel , that got it to compile. And here is what was captured from this line:
B4X:
    Log(bc.HexFromBytes(Token))
    Log(LastException)


(empty string)
<B4IExceptionWrapper: Error Domain=NSCocoaErrorDomain Code=3000 "no valid “aps-environment” entitlement string found for application" UserInfo={NSLocalizedDescription=no valid “aps-environment” entitlement string found for application}>
 
Upvote 0

stingrae

Member
Licensed User
Longtime User
You were right @Erel I was missing that line. Thank you. And now this procedure does give me a result in the Log from bc.HexFromBytes(Token)
B4X:
Sub Application_PushToken (Success As Boolean, Token() As Byte)
    Log($"PushToken: ${Success}"$)
    Dim bc As ByteConverter
    Log(bc.HexFromBytes(Token))
    Log(LastException)
End Sub
But unfortunately the problem is the same as my original post ... I can't send a message to the device, and the original Sub GetToken As String procedure still returns an empty string.

I'm using the B4J code to send a message and when I use this Token from Application_PushToken I get this error:
error:InvalidRegistration
From what I can see from Googling, this means that this is an unregistered Token. (https://stackoverflow.com/questions...ions-on-app-returns-invalidregistration-error)
Am I still doing something wrong?
Thanks & Regards.
 
Last edited:
Upvote 0

stingrae

Member
Licensed User
Longtime User
Hi @Erel , I never used the B4J to send because I couldn't get it to work for my Android and instead was able to write my own VB.NET code (below) to call the Firebase api and get the notification on the phone. (I need it to be called from this application to be of any use).

I've now been able to get it further with the following JSon based call:

B4X:
    Public Function NotificationSend_iOS2(ByVal MessageTitle As String, ByVal MessageBody As String) As Boolean
        Dim blnReturn As Boolean = True ' To do - check for failure
        Dim strServer_Api_Key As String = MyAPIKey
        Dim strSender_ID As String = MySenderID
        Dim strTopis As String = "/topics/ios_general"
        Dim nm As New NotificationMessage()
        nm.title = MessageTitle
        nm.Message = MessageBody
        nm.ItemId = 123
        Dim x As New JsonSerializerSettings()
        Dim strValue As String = JsonConvert.SerializeObject(nm, x)
        Dim strURL As String = "https://fcm.googleapis.com/fcm/send"
        Dim ourUri As New Uri(strURL)
        Dim tRequest As WebRequest = WebRequest.Create(strURL)
        tRequest.Method = "post"
        tRequest.ContentType = "application/json"
        tRequest.Headers.Add(String.Format("Authorization: key={0}", strServer_Api_Key))
        Dim strPost As String = "{""notification"":  { " &
                                """title"": ""Portugal vs. Denmark""," &
                                """body"": ""5 to 1""" &
                                "}," &
                                """content_available"": true, " &
                                """priority"": ""high""," &
                                """to"" : ""/topics/ios_general""" &
                                    "}"
        Dim byteArray As Byte() = System.Text.Encoding.UTF8.GetBytes(strPost)
        tRequest.ContentLength = byteArray.Length
        Dim dataStream As IO.Stream = tRequest.GetRequestStream()
        dataStream.Write(byteArray, 0, byteArray.Length)
        dataStream.Close()
        Dim tResponse As WebResponse = tRequest.GetResponse()
        dataStream = tResponse.GetResponseStream()
        Dim tReader As New IO.StreamReader(dataStream)
        Dim sResponseFromServer As String = tReader.ReadToEnd()
        Debug.Print(sResponseFromServer)
        tReader.Close()
        dataStream.Close()
        tResponse.Close()
        Return blnReturn
    End Function

I get a message ID returned but still no notification arrive on the phone. There are lots of posts on sites like Stack Overflow for this (example below) but I've had no such luck in finding out why.
https://stackoverflow.com/questions/38197787/firebase-message-not-received-while-using-api
 
Upvote 0

stingrae

Member
Licensed User
Longtime User
Ok, I did as you suggested and I have the B4J non-ui Push app but still have the same issue - message is sent, but doesn't appear on the iOS App.

Here is what is in the log of the B4J app:

B4X:
Waiting for debugger to connect...
Program started.
[jobname=fcm, success=true, username=
, password=, errormessage=, target=class b4j.example.main
, taskid=1, req=anywheresoftware.b4h.okhttp.OkHttpClientWrapper$OkHttpRequest@3c679bde, tag=java.lang.Object@16b4a017
, httputils2service=null]
{"message_id":5265305424540475402}

If I change a few letters of the API key in the B4J code then it throws an exception, so it's definitely correct there. And I've double-checked the API key in the iOS app too and it's correct.
I am at a loss as to why it's not working.
 
Last edited:
Upvote 0

stingrae

Member
Licensed User
Longtime User
ok ... i FINALLY have it working! :cool: and i'll put what i did here so others with the same issue can do the same.

Yesterday in an effort to get it working, I literally deleted all the keys and ended up breaking everything big-time ... so this morning I had to go through all the tutorials again, but with brand new apps, and with a greater understanding of everything, I got it to work.

In a previous thread to another question, I became confused between Firebase Analytics and Firebase Notifications. Someone asked something like "why are you trying to get analytics to work, if you just want notifications" ... So I had this line commented out:

B4X:
    analytics.Initialize

This morning I left that line in my new app and it's working. I'm guessing that was the missing link!

Thank you @Erel for your help. I'm now getting TokenIDs to appear in the code and notifications are arriving!
 
Upvote 0
Top