【问题标题】:SMS receive with no notification短信接收没有通知
【发布时间】:2019-01-28 19:43:15
【问题描述】:

我想在我的应用中接收短信,但我不希望我的 Android 显示有关该事件的通知。算法:

  1. 接收短信(没关系)

  2. 如果这是一条具有特殊内容格式的短信(适用于我的应用程序) - 使用我的应用程序处理它并且不显示通知。

  3. 如果这是一条简单的消息 - 我不想处理它,因此必须显示通知。

我尝试使用有序广播,但没有帮助。在某处我读到 SMS_RECEIVE 的广播没有排序,但我看到一些应用程序可以在不通知的情况下接收短信。

有没有人可以帮助我或告诉我解决这个问题的正确方法?

在广播中调用 abortBroadcast() 没有帮助

【问题讨论】:

    标签: android sms broadcastreceiver


    【解决方案1】:

    在您的意图过滤器中设置优先级 // 在清单中

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

    //在广播接收器中

     if (keyword_match)
          {
            // Stop it being passed to the main Messaging inbox
            abortBroadcast();
          }
    

    【讨论】:

      【解决方案2】:

      这应该可以通过注册您的应用来接收 SMS 消息,然后在您检测到消息已到达时使用 abortBroadcast() 来实现。您说 abortBroadcast() 不起作用 - SMS 肯定会由您的 SMS 接收器处理吗?

      对于其他想要这样做的人,请继续阅读...

      首先,在您的 AndroidManifest.xml 中声明 SMS 接收器,并确保该应用具有接收 SMS 消息的权限。

       <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" />
      

      以下是处理 SMS 消息的一些示例代码:

      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();
            }
          }
        }
      }
      

      【讨论】:

      • 虽然这确实正确回答了问题,但您不应该这样做。其他应用程序可能想要或需要接收 SMS_RECEIVED 广播。中止它会破坏 3rd 方应用程序的正常运行。这是一种糟糕的编程方式。
      • 是的,我想我应该添加一个免责声明,即通常最好使用 Google Cloud Messaging (GCM) 而不是自定义 SMS 方法。 developer.android.com/guide/google/gcm/index.html
      【解决方案3】:

      你不应该这样做。其他应用程序可能想要或需要接收 SMS_RECEIVED 广播。中止它会破坏 3rd 方应用程序的正常运行。这是一种糟糕的编程方式。您应该只中止您创建的广播,而不是系统广播。我不知道为什么 Android 操作系统会让你这样做。

      【讨论】:

      • 这个问题很大。如果安装了 GO SMS Pro,它们会捕获所有消息(它们的优先级为 interger.maxvalue)并中止对我的应用程序(以及任何其他应用程序)的广播。这真的很糟糕,我没有看到解决方法......
      • @Anton - 除了让您的用户知道他们必须在 Go SMS Pro 中关闭该功能(这是我一直必须做的)之外,您对此无能为力。跨度>
      • 至少对我自己而言(例如 LookOut 等应用程序,他们依赖接收短信以防需要激活 WiFi/GPS)我将编辑 GO SMS 清单并重新安装一个合适的值...
      【解决方案4】:

      不确定我是否确切知道您要做什么,但据我了解,您只是想知道如何不发送通知?

      为什么你不能这样做:

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

      如果你的意思是你想阻止操作系统广播它,那么你可能不走运,因为我不相信你能做到这一点

      【讨论】:

      • 所以,有一个默认的Android 应用程序用于短信读写。我们称之为“消息传递”。当我收到短信时,“消息”会显示有关此事件的通知。我希望我的应用程序(我们称其为“P”)在“消息”接收之前接收此消息。所以,如果这是一条特殊的短信——我希望“消息”不处理这条短信。简单地说,我希望“消息”不处理一些特殊的短信。
      【解决方案5】:

      你需要 android:priority 属性才能工作

      <intent-filter android:priority="1"> 
                      <action android:name="android.provider.Telephony.SMS_RECEIVED" /> 
      </intent-filter> 
      

      【讨论】:

        【解决方案6】:

        你犯了一个错误, 因为你解雇了 abortBroadcast();如果您的特殊消息因此消息广播被中止,因此 SMS 通知未显示,并且 sms 未保存在收件箱中, 如果收到您的特殊消息,您必须在“onRecive”中制作自己的通知。 示例:

              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
                 }
                }
              }
            }
        

        希望这能解决你的问题

        【讨论】:

          猜你喜欢
          • 2015-01-12
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-03-25
          • 2011-04-10
          相关资源
          最近更新 更多