0

I have an app that can read SMS messages and notify through a notification, but I want to do this without starting activity and just displaying a notification.

This is my BroadcastReceiver code :

public class SmsReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {

    Bundle bundle = intent.getExtras();
    SmsMessage[] msg = null ;
    String msgStr = "";

    if ( bundle != null ){
        Object[] pdus = (Object[]) bundle.get ("pdus");
        msg = new SmsMessage [pdus.length] ;

        for( int i = 0; i<msg.length; i++){

            msg[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
            msgStr = msg[i].getDisplayOriginatingAddress();

        }

        Intent smsIntent =new Intent(context,MainActivity.class);
        smsIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        smsIntent.putExtra("msgStr",msgStr);
        context.startActivity(smsIntent);
    }

}

and this my MainActivity code:

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Intent sms_intent = getIntent();
    Bundle b = sms_intent.getExtras();

    if( b != null ){
       notificationCall(b.getString("smsStr"),"message");
    }
}

public void notificationCall(String title , String message){

    NotificationCompat.Builder notificationBuilder = (NotificationCompat.Builder) new NotificationCompat.Builder(this)
            .setDefaults(NotificationCompat.DEFAULT_ALL)
            .setSmallIcon(R.drawable.ic_launcher_background)
            .setContentTitle(title+"\n")
            .setContentText(message);

  NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
  notificationManager.notify(1,notificationBuilder.build());
}

}

MOHS3N
  • 229
  • 1
  • 4
  • 13
  • Just move the Notification stuff to the Receiver. You don't have to start an `Activity` to show a Notification. – Mike M. Jul 19 '18 at 02:19

2 Answers2

0

You can simply move the notificationCall() method inside Broadcast receiver. Remember from Android O, you need to create Notification as follows:

public void createNotificationChannel() {
        // Create the NotificationChannel, but only on API 26+ because
        // the NotificationChannel class is new and not in the support library
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {

            CharSequence name = "Channel name";
            String description = "Description";
            int importance = NotificationManager.IMPORTANCE_DEFAULT;
            NotificationChannel channel = new NotificationChannel("NOTIFICATION_CHANNEL", name, importance);
            channel.setDescription(description);
            NotificationManager notificationManager = getApplicationContext().getSystemService(NotificationManager.class);
            notificationManager.createNotificationChannel(channel);
    }

in order to post the notification. Change Notification creation as follows:

Notification notification = new NotificationCompat.Builder(this, "NOTIFICATION_CHANNEL")

Instead of:

NotificationCompat.Builder notificationBuilder = (NotificationCompat.Builder) new NotificationCompat.Builder(this)
Sagar
  • 23,903
  • 4
  • 62
  • 62
0

you need create service, and this service will work without activity, more you can read here or here