OneSignal.isPushNotificationsEnabled doesn't return promised value - push-notification

After initialization OneSignal SDK my code trying to fetch user information with OneSignal.isPushNotificationsEnabled().
Sometimes it worked, but sometimes it doesn't return anything (pending).
The initialization code is fully copied from the official documentation.
Seems the OneSignal API doesn't answer that request. How it can be solved?

Solved. The OneSignal variable initialized before SDK import.

Related

Firebase Cloud Messaging Reports wrong

I am sending cloud-messages to my app but Firebase-CF-Reports tells me that they would not be received:
But I know for sure that some devices do receive them. e.g. my own. So something is going wrong here in the reports.
I read about this problem here and here but I already have an analytics-label that I send with my cloud-message.
This is how I sent my notifications with java-admin-sdk:
Message message = Message.builder()
.setTopic(topic)
.setAndroidConfig(AndroidConfig.builder()
.setPriority(AndroidConfig.Priority.HIGH)
.build())
.setApnsConfig(ApnsConfig.builder()
.setAps(Aps.builder()
.setMutableContent(true)
.setContentAvailable(true)
.build())
.putHeader("apns-push-type", "background")
.putHeader("apns-priority", "5")
.putHeader("apns-topic", "my.bundle.id")
.build())
.putData("\"content\"", contentString)
.putData("\"actionButtons\"", actionButtonsString)
.setFcmOptions(FcmOptions.withAnalyticsLabel("SendToAll"))
.build();
Also interesting is, that If I am not filtering for Platform/Channel (altough still filter only for my android app with Apps=) I get this:
But these numbers still don't make any sense. I also opened some notifications on my own device. And I can't believe that only 18 were received.
Has anyone an idea what I am doing wrong?
I use this fcm-sdk in my flutter app:
firebase_messaging: ^9.1.2
Despite I did not find this in the official documentation, I found information in this discussion in the comments to this answer here. Turns out that subscribing to a topic in FCM is not necessarily permanent. So don't subscribe users to a topic once. Instead do it on every app start, although it is
"not technically necessary. It may depend on your use case. For
example, if you want a global topic where all users are a member of,
you'd have to make sure that they are subscribed to it. Putting the
subscribe method when the app starts guarantees this."
-#AL.
Since I changed that, the Notifications are received by a lot more people than before. Only the open-count is still not working for me. It is always on zero.

Value of "WL.Client.Push" is coming as undefined

I am getting the value for WL.Client.Push as undefined when launching the application. Hence, the below is coming as false.
if(WL.Client.Push)
And that's the reason my push is not getting registered.
Please advise how can I define WL.Client.Push.
This normally happens when you try to use WL.Client.* APIs before the SDK initialization has been completed.
The right point to start using the WL.Client APIs is after the flow has entered the wlCommonInit() method.
Do note that IBM MobileFoundation 7.x is out of support, and you should move to MobileFoundation 8.0.
You can use MFPPush object to invoke Push APIs.
Documentation here.
A working sample can be found here.

Why do I get "Client is not yet ready to issue requests" - Firestore get() fails when called by Schedule function

In my Firebase project I have a functions.pubsub.schedule().onRun() that runs every 5 minutes to perform some calendar related tasks. It needs to look up in my Firestore collections and does so with a .get() query.
This has been working fine until sometime 2020-03-13 in the morning where the function started to throw
2020-03-13 09:36:02.326 CET scheduledHooks 1042277797598294
Error: INTERNAL ERROR: Client is not yet ready to issue requests.
at Firestore.get projectId [as projectId] (/srv/functions/node_modules/#google-cloud/firestore/build/src/index.js:401:19)
at Query.toProto (/srv/functions/node_modules/#google-cloud/firestore/build/src/reference.js:1556:42) at Query._get (/srv/functions/node_modules/#google-cloud/firestore/build/src/reference.js:1466:30)
at Query.get (/srv/functions/node_modules/#google-cloud/firestore/build/src/reference.js:1457:21)
at FirebaseActivitiesCollection.<anonymous> (/srv/functions/lib/collections/activities/FirebaseActivitiesCollection.js:32:40)
at Generator.next (<anonymous>) at /srv/functions/lib/collections/activities/FirebaseActivitiesCollection.js:8:71
at new Promise (<anonymous>) at __awaiter (/srv/functions/lib/collections/activities/FirebaseActivitiesCollection.js:4:12)
at FirebaseActivitiesCollection.getActivitiesByInterval (/srv/functions/lib/collections/activities/FirebaseActivitiesCollection.js:27:16)
I can't track that I have changed anything, could it be that Firebase made some changes that I should be aware of or am I missing an obvious clue in this error message?
My other Cloud Functions and the Firebase JavaScript SDK still work fine.
Extra info: The same code works on my other environment where I have not deployed. This of course led me to search for changes in the deployed code, but I can't find any!?
It seems by the error, that the project was not connected and initialized correctly. As per the official API here, this error occurs when:
Returns the Project ID for this Firestore instance. Validates that
initializeIfNeeded() was called before.
I would recommend you to check that. In case you are still facing, I would say for you to contact the Firebase Free Support. Since you mentioned that it was working until yesterday morning, it might be some change on their side.
Hope this helps!
This turned out to be a very simple matter of initialization of Firebase:
Before I had:
// Initialize firebase
admin.initializeApp(functions.config().firebase)
..And it worked fine until it didn't anymore.
Now I have:
// Initialize firebase
admin.initializeApp()
... And it works again
Got the same error while running some jest tests involving firebase. Problem was that I didn't have GOOGLE_APPLICATION_CREDENTIALS environment variable set.
Since I was running them locally on my machine, exporting this variable in terminal before running the tests was sufficient for my case:
export GOOGLE_APPLICATION_CREDENTIALS=/path/to/credentials/file.json

Flutter Crashlytics log caught exception

Looking for some clarification as to how one can log caught exceptions using flutter's firebase_crashlytics package.
If I understand correctly (and from running some sample code) Crashlytics.instance.log('text'); will only add logs to the next crash report, rather than send off a non-fatal issue itself.
I'm looking for functionality which is equivalent to Crashlytics.logException(e); on Android, e.g.
try {
throwException();
} catch (e) {
Crashlytics.logException(e);
}
which allows you to log caught exceptions so they appear as non-fatal issues in the Crashlytics dashboard.
Is this currently possible with flutter's firebase_crashlytics package?
Is calling Crashlytics.instance.recordError('text', StackTrace.current) the way to achieve this for now?
Many thanks!
Short answer, yes.
Crashlytics.instance.recordError() is the equivalent of Crashlytics.logException()
If you dig into the Flutter firebase_crashlytics source code, you can actually see what Android APIs are involved.
Flutter’s recordError() invokes the method Crashlytics#onError in the Android library.
And tracing Crashlytics#onError, you’ll see that it goes to Crashlytics.logException(exception);
Additional note, you’ll also notice why Crashlytics.instance.log() ”will only add logs to the next crash report”. These logs are added to a ListQueue<String> _logs which is then packaged into the next call of recordError()
A snippet of Flutter’s invocation of Crashlytics.logException():
_recordError(...) {
...
final String result = await channel
.invokeMethod<String>('Crashlytics#onError', <String, dynamic>{
'exception': "${exception.toString()}",
'context': '$context',
'information': _information,
'stackTraceElements': stackTraceElements,
'logs': _logs.toList(),
'keys': _prepareKeys(),
});
}
And some reference notes for Crashlytics.logException():
To reduce your users’ network traffic, Crashlytics batches logged
exceptions together and sends them the next time the app launches.
For any individual app session, only the most recent 8 logged
exceptions are stored.
To add to the accepted answer, Crashlytics.instance.recordError() has now been deprecated for the new method FirebaseCrashlytics.instance.recordError(exception, stack).
BONUS TIP:
I had this problem where all the logged exceptions are grouped under the same issue in Crashlytics dashboard. These might be different crashes of the same or different code components. Due to this, I had to manually go through each instance of the crash to verify.
From my own testing, I found out the grouping is based on the top-most line in the stack trace you passed into the method above. Luckily, Dart has an easy way to get the current stack trace using StackTrace.current.
So to properly group the issues: get the current stack trace at the time of the exception and pass it in FirebaseCrashlytics.instance.recordError(exception, stack).
Hope this helps someone out there, I looked everywhere on the internet for a similar issue but can't find any.

FieldValue.increment for Cloud Firestore in Flutter

I'm creating a android app using flutter and Firebase Firestore.
I would like to add that new FieldValue.increment functionality which is apperently available in Firebase since this april but i've got an error:
The method 'increment' isn't defined for the class 'FieldValue'.
Here is my code that i've tried:
onTap: (){
Firestore.instance.document('docRef').updateData({"numberOfDocs": FieldValue.increment(1)});
}
I'm just a beginner programmer but when i checked that field_value.dart file it's missing "increment" implementation.
So, is it right answer to say that flutter team didn't yet implemented that functionality?
I've seen a tutorial where increment is already used in some .js code but for me it's not working.
https://www.youtube.com/watch?v=8Ejn1FLRRaw
This has been added to the cloud_firestore Flutter plugin as of version 0.10.0.
You can use FieldValue.increment with a double (e.g. FieldValue.increment(1.0)) or an int (e.g. FieldValue.increment(2)).
Here is the relevant pull request.
If anyone reads this before the version is pushed to Pub: The link to the version will not yet work, but in the meantime you can use this Git commit, which contains a fix I did.
User that Import:
import 'package:cloud_firestore/cloud_firestore.dart';
For Increment:
await FirebaseFirestore.instance.collection('post').doc(postId).update({"like": FieldValue.increment(1)});
For Decrement:
await FirebaseFirestore.instance.collection('post').doc(postId).update({"like": FieldValue.increment(-1)});
As far as I can see in the change log for FlutterFire, the increment operator has not been added yet.
I added a feature request to the repo.

Resources