SMS receive with no notification

后端 未结 6 2047
旧时难觅i
旧时难觅i 2020-12-06 11:50

I want to receive a sms in my app, but I don\'t want my Android to show a notification about that event. Algorithm:

  1. Receive sms (it\'s ok)

  2. I

相关标签:
6条回答
  • 2020-12-06 12:14

    set priority in your intent-filter // in manifest

    <intent-filter android:priority="100">  /*your receiver get high priority*/
    

    // in broadcast receiver

     if (keyword_match)
          {
            // Stop it being passed to the main Messaging inbox
            abortBroadcast();
          }
    
    0 讨论(0)
  • 2020-12-06 12:18

    yo need android:priority attribute for that to work

    <intent-filter android:priority="1"> 
                    <action android:name="android.provider.Telephony.SMS_RECEIVED" /> 
    </intent-filter> 
    
    0 讨论(0)
  • 2020-12-06 12:18

    You are doing one mistake, because you fired abortBroadcast(); if your special message so the message broadcasting is aborted, therefore SMS notification is not Showing, and sms is not saving in Inbox, You have to make your own Notification in "onRecive" if your special message is received. Example:

          public class SMSReceiver extends BroadcastReceiver
        {
          @Override
          public void onReceive(Context context, Intent intent)
          {
            Bundle extras = intent.getExtras();
    
            Object[] pdus = (Object[])extras.get("pdus");
            for (Object pdu: pdus)
            {
              SmsMessage msg = SmsMessage.createFromPdu((byte[])pdu);
    
              String origin = msg.getOriginatingAddress();
              String body = msg.getMessageBody();
    
              // Parse the SMS body
              if (isMySpecialSMS)
              { // Stop it being passed to the main Messaging inbox
                abortBroadcast();
    NotificationManager notificationManager = (NotificationManager)context.getSystemService(context.NOTIFICATION_SERVICE);
            Notification notification = new Notification(R.drawable.ic_launcher, "New Message (My Messaging app)", System.currentTimeMillis());
            // Uses the default lighting scheme
            notification.defaults |= Notification.DEFAULT_LIGHTS;
            // Will show lights and make the notification disappear when the presses it
            notification.flags |= Notification.FLAG_AUTO_CANCEL | Notification.FLAG_SHOW_LIGHTS;
            Intent notificationIntent = new Intent(context, SplashActivity.class);
            PendingIntent pendingIntent =PendingIntent.getActivity(context, 0,
                    new Intent(context, SplashActivity.class), 0);
    
            Log.i("Wakeup", "Display Wakeup");
            PowerManager pm = (PowerManager)context.getApplicationContext().getSystemService(Context.POWER_SERVICE);
            WakeLock wakeLock = pm.newWakeLock((PowerManager.SCREEN_BRIGHT_WAKE_LOCK | PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP), "Phone WakeUp");
            wakeLock.acquire();
    
            notification.setLatestEventInfo(context, "My Messaging App(New Message)",msg, pendingIntent);
            //notification.sound.
            notification.defaults |= Notification.DEFAULT_SOUND;
            notificationManager.notify(9999, notification);
              }
             else{
            //Continue Broadcasting to main android app
             }
            }
          }
        }
    

    I hope this will solve your Problem

    0 讨论(0)
  • 2020-12-06 12:23

    You should not do this. Other apps might want or need to receive the SMS_RECEIVED broadcast. Aborting it will disrupt 3rd party apps from running properly. This is a bad way to program. you should only abort broadcasts that you create, not system broadcasts. I don't know why the Android OS lets you do this.

    0 讨论(0)
  • 2020-12-06 12:24

    This should be possible by registering your app to receive SMS messages and then using abortBroadcast() when you detect your message has arrived. You say abortBroadcast() doesn't work - is the SMS definitely getting handled by your SMS receiver?

    For anybody else wanting to do this, read on...

    First, declare the SMS receiver in your AndroidManifest.xml and make sure the app has permission to receive SMS messages.

     <receiver android:name="mypackage.SMSReceiver">
       <intent-filter>
         <action android:name="android.provider.Telephony.SMS_RECEIVED"/>
       </intent-filter>
     </receiver>
    
    <uses-permission android:name="android.permission.RECEIVE_SMS" />
    

    Here's some example code to handle the SMS messages:

    public class SMSReceiver extends BroadcastReceiver
    {
      @Override
      public void onReceive(Context context, Intent intent)
      {
        Bundle extras = intent.getExtras();
    
        Object[] pdus = (Object[])extras.get("pdus");
        for (Object pdu: pdus)
        {
          SmsMessage msg = SmsMessage.createFromPdu((byte[])pdu);
    
          String origin = msg.getOriginatingAddress();
          String body = msg.getMessageBody();
    
          // Parse the SMS body
          if (isMySpecialSMS)
          {
            // Stop it being passed to the main Messaging inbox
            abortBroadcast();
          }
        }
      }
    }
    
    0 讨论(0)
  • 2020-12-06 12:24

    not sure if i know exactly what you are trying to do but from what i understand you just want to know how to not send a notification?

    why cant you just do:

    If(instance 2){
    //do your processing
    }else{
    //send notification
    }
    

    if you mean you want to block the OS from broadcasting it then you might be out of luck because i dont believe you can do that

    0 讨论(0)
提交回复
热议问题