Skip to content
Mahdi Malvandi edited this page Sep 7, 2019 · 1 revision

Functionalities of Pushe

Topics

You can add a user to a specific group. In order to do this you need to use topics. To subscribe a user to a topic:

Pushe.subscribe("topic_name");

And to undo this:

Pushe.unsubscribe("topic_name");

PusheId

Pushe id is an id that makes a device unique and can be used to identifiy devices (users) and not app users. To get that id:

Pushe.getPusheId().then((pusheId) {
  // Deal with id
});

Check initialization of Pushe

If you want to see wether pushe is registered to server or not:

Pushe.isPusheIntialized().then(isIt => print('Is it? $isIt'));

Notification listeners

To listen to different event of a notification (such as receive, click, etc), you can use this codes:

Pushe.setNotificationListener(
      onReceived: (notificationData) { /* Your code */ },
      onClicked: (notificationData) { /* Your code */ },
      onDismissed: (notificationData) { /* Your code */ },
      onButtonClicked: (notificationData, clickedButton) { /* Your code */ },
      onCustomContentReceived: (customContent) => { /* Your code */ },
    );

Send notification using code

It's possible to send push notification to the device using code [To a device having this app installed].
The only thing you need is the PusheId of that device.

Send simple notification
var pusheId = "some_id"; // Pass await Pushe.getPusheId() to send a notification to this device.
Pushe.sendSimpleNotifToUser(pusheId, 'title', 'content');
Send advanced notification
var pusheId = "some_id"; // Pass await Pushe.getPusheId() to send a notification to this device.
Pushe.sendAdvancedNotifToUser(pusheId, '{"title":"Hello","content":"Have a good day!"}'); 

For complete json example refer to Pushe Restful api doc.

Use it along with FireBase messaging (or other fcm services)

If you also use firebase messaging plugin at your project, you might have problems receiving and sometimes losing the message.
Since Pushe uses FCM and one app can only have one firebase service, using both is not possible unless you do this:

  1. Remove Pushe service and Firebase service (or the service you desire to use) from your AndroidManifest tree using this code (add it to application block):
<service
   android:name="co.ronash.pushe.fcm.FcmService" 
   tools:node="remove" />

<service
   android:name="io.flutter.plugins.firebasemessaging.FlutterFirebaseMessagingService" 
   tools:node="remove" />

Make sure xmlns:tools="http://schemas.android.com/tools" exists in the manifest tag attributes. Otherwise tools is not known to app.

  1. Create a native java class (for example MyFcmService) and extend the class handles the messages of Firebase (or that other service). In this it's io.flutter.plugins.firebasemessaging.FlutterFirebaseMessagingService.
public class MyFCMService extends FlutterFirebaseMessagingService {
}

Override onNewToken, onDeletedMessages, onSendError, onMessageSent and onMessageReceived and the codes below to them:

public class MyFCMService extends FlutterFirebaseMessagingService {
    @Override
    public void onNewToken(String s) {
        co.ronash.pushe.Pushe.getFcmHandler(this).onNewToken(s);
        super.onNewToken(s);
    }
    @Override
    public void onDeletedMessages() {
        co.ronash.pushe.Pushe.getFcmHandler(this).onDeletedMessages();
        super.onDeletedMessages();
    }
    @Override
    public void onSendError(String s, Exception e) {
        co.ronash.pushe.Pushe.getFcmHandler(this).onSendError(s, e);
        super.onSendError(s, e);
    }
    @Override
    public void onMessageSent(String s) {
        co.ronash.pushe.Pushe.getFcmHandler(this).onMessageSent(s);
        super.onMessageSent(s);
    }
    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        if (!co.ronash.pushe.Pushe.getFcmHandler(this).onMessageReceived(remoteMessage)) {
            // It is for fire base, otherwise the condition will handle the message for Pushe
            super.onMessageReceived(remoteMessage);
        }
    }
}
  1. Add your service tag in the AndroidManifest to let the Plugin handle both messaging services and make them work together.
<service android:name=".MyFcmService">
    <intent-filter>
        <action android:name="com.google.firebase.MESSAGING_EVENT" />
    </intent-filter>
</service>

Note: If you used another service other than Firebase, extend it's messaging class and not firebase's.

Note: Callbacks only work when app is not fully closed and flutter is still running under the hood. So when app is not opened or force stopped, listeners will not be called.

More Info

For detailed documentations visit https://pushe.co/docs/flutter/