Notification - mesosfer/Mesosfer-Cubeacon-iOS GitHub Wiki

Mesosfer provide a specialized bucket Notification that automatically handles much of the functionality required for notification management.

Objects and Types

Here is some predefined Notification object :

  • beacon => MesosferBeacon, define the beacon object of a notification.
  • description => String, define the description of notification.
  • event => String, define the event name of a notification.
  • type => String, define the event type of a notification.
  • isEnabled => Boolean, define the state of notification whether it is enabled or not.
  • message => Object, define custom object of a notification.

Saving Notifications

Let’s say you want to save the MFNotification described above to the Mesosfer Cloud. The interface is similar to a NSDictionary, plus the saveAsyncWithBlock: method:

// create new notification object using beacon objectId
MFNotification *notif = [MFNotification notificationWithBeaconId:@"beaconObjectId"];
// set description
notif.notifDescription = @"Monday-Notification";
// set state whether it is enabled or not
notif.isEnabled = true;
// set beacon event
notif.event = MFBeaconEventEnter;
// set notification type
notif.type = MFNotificationTypeText;
// set custom objects
notif[@"alertTitle"] = @"Hello, good morning.";
notif[@"alertMessage"] = @"How is your weekend? It's great, isn't it? Have a nice day.";
notif[@"timestamp"] = [NSDate date];
notif[@"object"] = [NSDictionary dictionary];
notif[@"array"] = [NSArray array];
// execute save async using block
[notif saveAsyncWithBlock:^(BOOL succeeded, NSError * _Nullable error) {
    if (!succeeded) {
        NSLog(@"Failed to save notification, cause: %@", error);
        return;
    }
    
    NSLog(@"Notification saved.");
}];

Retrieve Notifications

If you need to fetch a Notification with the latest data that is in the cloud, you can call the fetchAsyncWithBlock: method like so:

// create notification from existing objecId
MFNotification *notif = [MFNotification notificationWithObjectId:@"notificationObjectId"];
// fetching the notification data
[notif fetchAsyncWithBlock:^(id  _Nullable object, NSError * _Nullable error) {
    if (error) {
        NSLog(@"Failed to fetch notification, cause: %@", error);
        return;
    }
    
    NSLog(@"Notification fetched.");
}];

This will automatically update notif with the latest data from cloud.

Querying Notification

There are many other ways to retrieve data with MFQuery. You can retrieve many data at once, put conditions on the data you wish to retrieve.

Basic Query

In many cases, there is a condition that need to specify which datas you want to retrieve. The MFQuery offers different ways to retrieve an array of MFNotifications. The general pattern is to create a MFQuery, put conditions on it, and then retrieve a NSArray of matching MFNotifications using the findAsyncWithBlock: method. For example, to retrieve MFNotifications data with a type, use the whereKey:equalTo: method to constrain the value for a key:

MFQuery *query = [MFNotification query];
[query whereKey:NOTIFICATION_KEY_TYPE equalTo:@(MFNotificationTypeText)];
[query findAsyncWithBlock:^(NSArray * _Nullable objects, NSError * _Nullable error) {
    if (error) {
        NSLog(@"Failed to query notification, cause: %@", error);
        return;
    }
    
    NSLog(@"Found notification: %lu", objects.count);
}];

Query Constraint

Refer to this link to learn more about query constraint.

Update Notification

After getting the MFNotification data, you can update your data that stored in cloud using method saveAsyncWithBlock:.

MFNotification *notif; // fetched notification data
// update notification data
notif.event = MFBeaconEventExit;
notif.type = MFNotificationTypeImage;
notif[@"timestamp"] = [NSDate date];
notif[@"urlImageIOS"] = @"http://image.com/image-ios-640x1136.png";
// execute save notification data
[notif saveAsyncWithBlock:^(BOOL succeeded, NSError * _Nullable error) {
    if (!succeeded) {
        NSLog(@"Failed to update notification, cause: %@", error);
        return;
    }
    
    NSLog(@"Notification updated.");
}];

Delete Notification

To delete a MFNotification from the Mesosfer Cloud, use method deleteAsyncWithBlock: :

// the current notification object to delete
MFNotification *notif;
// execute delete notification async
[notif deleteAsyncWithBlock:^(BOOL succeeded, NSError * _Nullable error) {
    if (!succeeded) {
        NSLog(@"Failed to delete notification, cause: %@", error);
        return;
    }
    
    NSLog(@"Notification deleted.");
}];