Notification Management

The following steps are used to obtain and manage additional delivery data from the sent Push Notifications.

Step 1: Put LocalBroadcastReceiver in Activity project

Put LocalBroadcastReceiver in Activity file project:

...
import com.messaging.sdk.Messaging;
public class MainActivity extends AppCompatActivity{
   ...
          ...
       @Override
   protected void onStart() {
       super.onStart();
       LocalBroadcastManager.getInstance(this).registerReceiver(mReceiver,
               new IntentFilter(Messaging.ACTION_FETCH_DEVICE));
               .....
   }

using the correspondingIntentfilter, example: Messaging.ACTION_FETCH_DEVICE.

Then you can create a method:

public void dothisForRequestUser() {
       Messaging.fetchDevice(false);
   }

Using fetchDevice device delivers the instance of a device by internal memory, local memory or a service.

and Finally in OnDestroy cycle live of Activity:

 @Override
protected void onDestroy() {
  LocalBroadcastManager.getInstance(this).unregisterReceiver(mReceiver);
   super.onDestroy();
}

This method letunregister receivers when activity finishes.

For get a response from the fetch device request, you must implement a LocalBroadcastReceiver in the activity:

private BroadcastReceiver mReceiver=new BroadcastReceiver() {
       @Override
       public void onReceive(Context context, Intent intent) {
           boolean hasError=intent.getBooleanExtra(Messaging.INTENT_EXTRA_HAS_ERROR,true);
           
           if (!hasError ) {
               Serializable data=intent.getSerializableExtra(Messaging.INTENT_EXTRA_DATA);
               if(intent.getAction().equals(Messaging.ACTION_FETCH_DEVICE)&& data!=null){
                   messagingDevice = (MessagingDevice) data; //you can cast this for get information
                   //or messagingDevice = MessagingDevice.getInstance();
                   .....
               }else if(intent.getAction().equals(Messaging.ACTION_FETCH_USER)&& data!=null){
                   messagingUser =(MessagingUser) data;
                   //or messagingUser = MessagingUser.getInstance();
                   .......
               }else if(((intent.getAction().equals(Messaging.ACTION_GET_NOTIFICATION))||(intent.getAction().equals(Messaging.ACTION_GET_NOTIFICATION_OPENED)))&& data!=null){
                   messagingNotification=(MessagingNotification)data;
                   .....
               }else if(intent.getAction().equals(Messaging.ACTION_SAVE_DEVICE)&& data!=null) {
                   messagingDevice = (MessagingDevice) data; //you can cast this for get information
                   //or messagingDevice = MessagingDevice.getInstance();
                  ......
               }else if(intent.getAction().equals(Messaging.ACTION_SAVE_USER)&& data!=null) {
                   messagingUser =(MessagingUser) data; //you can cast this for get information
                   //or messagingUser = MessagingUser.getInstance();
                   
           }else if(intent.getAction().equals(Messaging.ACTION_REGISTER_DEVICE) ) {
                   messagingDevice = (MessagingDevice)data;
                   //or messagingDevice = MessagingDevice.getInstance();
               ......
               } 
               .....
           }else{
               Toast.makeText(getApplicationContext(),"An error occurred on action "
                       +intent.getAction(),Toast.LENGTH_LONG).show();
           }
           ......
       }
   };
  //you can see all the implementation in example app

Step 2: Put BroadcastReceiver in app project

...
import …...

public class MessagingNotificationReceiver extends BroadcastReceiver{
   ...
   
          ...
       @Override
   public void onReceive(Context context, Intent intent) {
      boolean hasError=intent.getBooleanExtra(Messaging.INTENT_EXTRA_HAS_ERROR,true);
       
       if (!hasError ) {
           String action=intent.getAction();
           Serializable data = intent.getSerializableExtra(Messaging.INTENT_EXTRA_DATA);
           if(intent.getAction().equals(Messaging.ACTION_GET_NOTIFICATION)&& data!=null){
           ........
           }
           }
               .....
   }
   //to send data to Activity if app is foreground
   private void sendEventToActivity(String action,Serializable something, Context context) {
       
       if(something!=null) {
           Intent intent = new Intent(action);
           intent.putExtra(Messaging.INTENT_EXTRA_DATA, something);
           intent.putExtra(Messaging.INTENT_EXTRA_HAS_ERROR, something == null);
           LocalBroadcastManager.getInstance(context).sendBroadcast(intent);
       }else{
           //error
       }
   }
	

It is important to use, please declare BroadcastReceiver in Manifest.xml of app project, example:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools"
   package="com.ogangi.Messangi.SDK.Demo">

   <application
       .....
       <activity 
       </activity>
       <receiver
           android:name=".MessagingNotificationReceiver"
           android:enabled="true"
           android:permission="${applicationId}.permission.pushReceive"
           android:exported="false"
           tools:ignore="Instantiatable">
           <intent-filter>
               <action android:name="com.messaging.sdk.PUSH_NOTIFICATION"/>
           </intent-filter>
       </receiver>
   </application>
   <permission
       android:name="${applicationId}.permission.pushReceive"
       android:protectionLevel="signature" />
   <uses-permission android:name="${applicationId}.permission.pushReceive" />
</manifest>

Step 3: Receive Notifications in Background

To handle Notifications in Background, use this code in Activity:

//for handle notification from background
       Bundle extras=getIntent().getExtras();
       if(extras!=null){
           isBackground=extras.getBoolean("isInBackground",false);
           if(isBackground) {
               Serializable data = extras.getSerializable(Messaging.INTENT_EXTRA_DATA);
               messagingNotification=(MessagingNotification)data;
               .....
           }else {
               //to process notification from background mode
   MessagingNotification notification=Messaging.checkNotification(extras);
               .......
           }
       }
Was this article helpful?
0 out of 0 found this helpful

Comments

0 comments

Article is closed for comments.