Jithish P N
Jithish P N

Reputation: 2160

Broadcast receiver stop triggering when swipe clear the app

Broadcast receiver stop triggering "onReceive(Context context, Intent intent)" method after clear(swipe) app from recent list , how to solve this issue ?

Manifest code

   <receiver
    android:name="*.receiver.TestReceiver"
    android:enabled="true"
    android:exported="true"
    android:permission="com.google.android.c2dm.permission.SEND" >
    <intent-filter>
        <action android:name="com.google.android.c2dm.intent.RECEIVE" />
        <action android:name="com.google.android.c2dm.intent.REGISTRATION" />
        <action android:name="com.google.android.c2dm.intent.REGISTER" />
        <action android:name="android.intent.action.BOOT_COMPLETED" />

        <action android:name="android.intent.action.USER_PRESENT" />
        <category android:name="pacakge" />
    </intent-filter>
</receiver>

Receiver Code

public void onReceive(Context context, Intent intent) {
    LogUtils.LOGD("notfy",  "Receiver : ");
    if(intent!=null){
        Bundle extras = intent.getExtras();
        if(extras!=null) {
            try {
                String regId = extras.getString("registration_id");
                String message = extras.getString("message");
                String qsid = extras.getString("id","");
                String type = extras.getString("type","");
                // extras.getString("aps");


                       /*code*/



            } catch (Exception e){

                LogUtils.LOGD("notfy",  "onReceive last exception: " + e.getMessage());
            }
        }


    }
}

Upvotes: 2

Views: 1385

Answers (2)

Avinash Roy
Avinash Roy

Reputation: 973

In main app start/stop the service

Intent service = new Intent(context, MyService.class);
context.startService(service);
...
Intent service = new Intent(context, MyService.class);
context.stopService(service);

service

  public class MyService extends Service
    {
     private static BroadcastReceiver m_Receiver;

     @Override
     public IBinder onBind(Intent arg0)
     {
      return null;
     }
    @Override
  public int onStartCommand(Intent intent, int flags, int startId) {
    return START_STICKY;
    }

     @Override
     public void onCreate()
     {
      Receiver();
     }

     @Override
     public void onDestroy()
     {
     unregisterReceiver(m_Receiver);
     }

     private void Receiver()
     {
     m_Receiver = new BroadcastReceiver()
      {
       @Override
       public void onReceive(Context context, Intent intent)
       {
        //place all ur stuff here
       }
      }

     }
    }

Upvotes: 1

yanivtwin
yanivtwin

Reputation: 625

It looks like the problem is on the server side ,

https://firebase.google.com/docs/cloud-messaging/concept-options

There are two types of messages

With FCM, you can send two types of messages to clients:

Notification messages, sometimes thought of as "display messages." Data messages, which are handled by the client app.

Basically change from notification to data on your server , this will call OnRecieve when app is closed.

Check the link for more info and examples

Upvotes: 0

Related Questions