Ionic 3 adding subscriptions for notification topic in a loop - ionic-framework

In my Ionic app, I am trying to dynamically subscribe to topics based on the user's preference. So every time the app loads I am trying to re-subscribe them to their topics. I noticed that without resubscribing them, my callback code is not executed, and thus no popup notification is generated, when a notification comes for that given topic. Rather, the notification just ends up in the notification drawer (android).
So, the app boots up, I request the list of sub topics from my db and loop over each one calling the below function to subscribe:
addBillNotificationSubscription(topic: string){
console.log("Adding a bill notification subscription for topic: " + topic)
this.pushObject.subscribe(topic).then((res:any) =>{
console.log("Received test channel notification for topic: " + topic)
if (res.additionalData.foreground) {
this.showNotificationPopup(res);
}
})
console.log("Done Adding subscription")
}
Now when I call this in a loop, only the first iteration ever gets executed. I've double checked the elements in the list, there are several and even print them out in another loop right before the loop to subscribe. I'm assuming that I'm completely not understanding some fundamental thing, but I'm stumped. There's not much documentation (that I've found) on the proper way to even do these topic subscriptions or example use cases.
I've fully tested one topic subscription and have gotten it working in case the question arises.

Related

How to setup user notifications with multiple recipients and read status in MongoDB (Meteor app)

In my Meteor app I have a mongo collection Notifications for user notifications, and I want to keep one document for one notification, even in case of multiple recipients. In each notification I have an array with users IDs. And I need to keep tack of read status for each user.
I tried to use an additional array to record who've read the notification like this:
{
...notification,
users: [id1,id2,id3],
read: [id2]
}
But two arrays cannot be indexed properly as I understand the docs. So my current approach is to have separate collection of UnreadNotifications with the documents referencing actual notifications by IDs and linking them to the users like this:
{
notificationID: notificationid1,
userID: id2
}
When the new notification created also a number of unread notification documents created for all the related users. When user reads a notification, the document is removed from mongo. Does this approach make sense? What are possible downsides?
The one issue I can think about is that user will probably have to fetch all of his unread notifications at once to be able to display witch of the real notifications are read. But it doesn't sound that bad.
Ok, think from a client perspective. I need to get my notifications in the lightest way possible. I don't want other people's notifications, I don't want to construct my data via aggregations.
Your Insert/create notification Meteor Method can have 2 function:
Insert the actual notification. If you have 10 people, 100 people, that is 10 ... 100 notifications.
On the user, write the increment of a notification count and eventually have it published with the other user details so that you can have it displayed.
When you open your notifications, you have a couple of options:
Read them all (write to db a "seen" or "read" field = Date.new() for these notifications. This field will help you style the notification as well seen - not seen.
Read on click of notification if in your UX a notification has to be opened. For example, I think in FB you have to click a notification to read it while in LinkedIn, opening notifications reads them all at once.
Use a library for in-view and only read those notifications that are in view, as you scroll in the list of notifications.
1 is the easiest to do and you also reset the counter on User to 0.
In summary, your insert notification method would be something like:
'methodName': async ({ notificationObject, userIds }) => {
...
userIds.forEach(id => {
- insert your notification
- increment user counter
- send a push notification
- or send an email
})
}
If you do this server-side and need to insert notifications from multiple methods such as: someone liked your something, someone sent a message to you etc... these are multiple methods that will be calling your insert notification method. In this case, if everything happens server side, better have the notification insert as a function instead of a method.

Flutter Blocs quick events leads to data loss

I am new to flutter and the famous flutter_bloc library. I have created a chat system based on the BLoC pattern :
A widget fires an event to chatBloc
The chatBloc catches the event, process it and call chatRepository
ChatRepository adds the message as a draft in its messageList variable and then sends the message to the API
ChatRepository receives the message from the API when it is done and replaces the draft within its messageList variable
ChatBloc yields its final state and the page rebuilds
Everything works fine as long as no more than one message is processed at a time.
If my button widget fires two quick events, they will overlap and so the second event will fire while the first event is still in progress. By the time when the second event starts, the first message is still a draft, so when message 2 completes and chatRepository updates its messageList, it will treat message 1 as a draft even if it has been validated.
At this point I'm asking myself if I made a mistake or if I'm trying to do it the wrong way.
Would it be better to create a queue and process messages in the background one by one ?
Thank you for your help !

JSQMessagesViewController message observer duplicating messages

I have a simple application using firebase. You login and it takes you to a chatroom / channel board where you can select the topic you would like to post in.
Everything works perfectly, writes to firebase, updates the message board.
The issue comes in after you post in one topic, back out (segue) and then select a different or same topic to post in. It duplicates (or triplicates depending on the number of times you exist and go back in) on the message board, but only writes to firebase the one time.
I tried to messages.removeAll() on Load, but it did not help.
Thank you,
I can offer more help if there are some code but from your problem I think the reason is you did not clear observer before back out of topic. Something like this:
conversationReference = Database.database().reference().child("conversations/{conversationId}")
//observe messages ...
//then before moving out
conversationReference.removeAllObservers()

How to receive cumulocity real time notifications with SmartREST?

We use the cumulocity REST API. Regular real time notifications work, e.g. we subscribe to /alarms/*, start our connection/polling loop and when we create an alarm we receive the expected JSON. We did not install any specific modules or statements, it just works.
But when we try to do the same with SmartREST we receive this error, as soon as the alarm is created:
40,,/alarms/177649296,Could not find any templates subscribed for the channel
Following the reference guide (http://cumulocity.com/guides/reference/smartrest/) we tried it like this, where all requests have the same X-Id-header and all requests result in the expected http status 200 and no error messages, except for the last one:
Register a smart response template by doing a POST to /s
Body: 11,102,,,$.channel
Handhake: POST to /cep/realtime
Body: 80
Response is our clientId (e.g. 191het1z38bp7iq1m96jqqt8jnef)
Subscribe: POST to /cep/realtime
Body: 81,191het1z38bp7iq1m96jqqt8jnef,/alarms/*
Connect: POST to /cep/realtime
Body: 83,191het1z38bp7iq1m96jqqt8jnef
In the normal REST case the notification consists of a JSON array with 2 elements, both of which have a property "channel". So that is what we would expect from our response template. Instead, we get the aforementioned error 40.
Is our response template wrong? Is it not properly matched by the X-Id? What does it mean, that there are no "templates subscribed for the channel"? The subscriptions are done for a clientId, and not for a specific response template, and the templates are supposed to be matched automatically anyway. So probably "template" means "X-Id" here? The documentation seems ambiguous as to the meaning of that word. But anyway, we did use the same X-Id header in all of the requests.
Any pointer about what we're doing wrong would be appreciated, since we tried pretty much anything by now.
The SmartREST protocol was developed for a IoT-device <-> platform communication. So there was never any design around using it to subscribing to realtime data (except of course for the operations a device needs) as usually devices to not need subscribe to the data that they created themselves.
That said it is possible to use it but with a couple of limitations. Your approach is basically correct but there is one problem with the subscription. The wildcard subscriptions will not work with SmartREST because on subscription it links your X-Id with the channel you subscribed to but there is never a message published on the channel /alarms/*. Thus this kind of weird error message that said that there was no template subscribed for the channel the alarm appeared on. Inside CometD you still receive the alarm because of the wildcard subscription but the SmartREST part does not work.
The messages are published on the channel with the deviceId (e.g. /alarms/12345).
If you subscribe to /alarms/12345 it will work. You can of course subscribe to as many channels as you want but wildcard subscription won't work.
Regarding the templates you need to know the following. The SmartREST parsing is not done on the raw JSON of CometD but on the payload inside it (e.g. the alarm). So a template for an alarm could look like this:
11,500,,$.severity,$.id,$.type,$.severity
This would trigger only if the object has a severity and would return id, type and severity.

Merge notifications with OneSignal

Using Ionic and OneSignal.
I am developing a messaging service in my app which sends push notifications when a new message is received. Ok so far.
But, I would like to merge notifications if they exceed a certain number, to avoid spamming the user with to much notifications.
Let's say if someone answer to one of my message with 3 different messages and that my app is in background, I would like the following to happen:
receive 1 notification
receive 1 other distinct notification
display only one notification which merge the previous two + a new one
Someone know, if possible and how?
I have just implemented something similar. You can look at the following documentation link
stacked notification
I just set the android_group parameter in all of my one signal api call and send a string key on every notification message.
{
app_id = "xxxxxxxxxxxx"; // one signal app id
headings = "One Signal Title Test";
content = "The demo test message";
android_group = "demo-application";
included_segments = "Active Users"; // to send notification to all active users
}
use postman to test
There's couple of parameters that you can set to group messages.
on Android you can use android_group witch accept a string and based on docs:
Notifications with the same group will be stacked together using Android's Notification Grouping feature.
And on iOS you can use thread_id witch also accept a string:
This parameter is supported in iOS 12 and above. It allows you to group related notifications together. If two notifications have the same thread-id, they will both be added to the same group.
There are couple more parameters that might be useful that you can check on Grouping & Collapsing OneSignal Docs