Search code examples
androidnotificationssmsstatusbar

Status bar notification on incoming SMS in Android?


I am a beginner in Android programming. I am trying to implement a notification for incoming SMS in android device. However, I am able to create a toast for the same but unable to implement a status bar notification. Can nyone help me ?

For now I have coded this following code :

 @Override
        public void onReceive(Context arg0, Intent arg1) 
        {

    //      Log.i(TAG,"OnReceive ++      ");
            Bundle bndl = arg1.getExtras();
            SmsMessage[] msg = null;

            if (null != bndl)
            {
                //---retrieve the SMS message received---
                Object[] pdus = (Object[]) bndl.get("pdus");
                msg = new SmsMessage[pdus.length];         
                for (int i=0; i<msg.length; i++)
                {
                    msg[i] = SmsMessage.createFromPdu((byte[])pdus[i]);             
                    str += "SMS From " + msg[i].getOriginatingAddress();                   
                    str += " :\r\n";
                    str += msg[i].getMessageBody().toString();
                    str += "\n";
                    context = arg0;
                }

                //---display incoming SMS as a Android Toast---
                Toast.makeText(arg0, str, Toast.LENGTH_SHORT).show();
                @SuppressWarnings("unused")
                class MynotificationMain extends Activity 
                {
                    int mNotificationId = 001;

                    @Override
                    protected void onCreate(Bundle savedInstanceState) 
                    {
                        super.onCreate(savedInstanceState);
                        //setContentView(R.layout.activity_mynotification_main);
                        NotificationCompat.Builder  mBuilder = new   NotificationCompat.Builder(context);
                          mBuilder.setContentTitle("New Message");
                          mBuilder.setContentText(str);
                          mBuilder.setTicker("New Message Alert!");
                          mBuilder.setSmallIcon(R.drawable.notification);

                        Intent resultIntent  = new Intent(this, MainActivity.class );
                        PendingIntent resultPendingIntent = PendingIntent.getActivity(this, 0, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT);

                        mBuilder.setContentIntent(resultPendingIntent);

                        NotificationManager mNotifyMgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
                        mNotifyMgr.notify(mNotificationId, mBuilder.build());

                        MapsFragment obj = new MapsFragment();
                        obj.onLocationChanged(null);
                    }
                }
            }

        }
    }

Solution

  • Why do you make activity inside a method? it should be

    @Override
    public void onReceive(Context arg0, Intent arg1) 
    {
     Bundle bndl = arg1.getExtras();
     SmsMessage[] msg = null;
     if (null != bndl)
     {
       //---retrieve the SMS message received---
       Object[] pdus = (Object[]) bndl.get("pdus");
       msg = new SmsMessage[pdus.length];         
       for (int i=0; i<msg.length; i++)
       {
         msg[i] = SmsMessage.createFromPdu((byte[])pdus[i]);             
         str += "SMS From " + msg[i].getOriginatingAddress();                   
         str += " :\r\n";
         str += msg[i].getMessageBody().toString();
         str += "\n";
       }
    
       //---display incoming SMS as a Android Toast---
       Toast.makeText(arg0, str, Toast.LENGTH_SHORT).show();
       //---Make a notification          
       NotificationCompat.Builder  mBuilder = new   NotificationCompat.Builder(context);
       mBuilder.setContentTitle("New Message");
       mBuilder.setContentText(str);
       mBuilder.setTicker("New Message Alert!");
       mBuilder.setSmallIcon(R.drawable.notification);
    
       Intent resultIntent  = new Intent(this, MainActivity.class );
       PendingIntent resultPendingIntent = PendingIntent.getActivity(arg0, 0, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    
       mBuilder.setContentIntent(resultPendingIntent);
    
       NotificationManager mNotifyMgr = (NotificationManager) arg0.getSystemService(arg0.NOTIFICATION_SERVICE);
       mNotifyMgr.notify(mNotificationId, mBuilder.build());
    
     }
    }