用户收到短信时创建通知

时间:2018-05-18 01:53:24

标签: android notifications broadcastreceiver

我希望在收到特定短信“A”或“B”时收到通知 我无法在broadcastreceive中创建通知。那我该怎么办?

public class ReceiveMessage extends BroadcastReceiver {
    final SmsManager mysms = SmsManager.getDefault();
    final String ALERT="A";
    final String ALERT2="B";

    public void onReceive (Context context, Intent intent) {
        Bundle myBundle = intent.getExtras();
        try {
            if (myBundle != null) {
                final Object [] pdus = (Object[])myBundle.get("pdus");
                String format = myBundle.getString("format");
                for (int i = 0; i < pdus.length ; i++) {
                    SmsMessage mynewsms= SmsMessage.createFromPdu((byte[]) pdus[i], format);
                    String sms = mynewsms.getMessageBody();
                    Toast.makeText(context, "SMS"+mynewsms, Toast.LENGTH_SHORT).show();
                    if (sms.equals(ALERT) {
                        Toast.makeText(context,"............",Toast.LENGTH_LONG).show();
                    }
                    if (sms.equals(ALERT2) {
                        Toast.makeText(context,"Your child is out save zone",Toast.LENGTH_LONG).show();
                    }
                }
            }
        } catch (Exception ex) {
        }
    }
}

1 个答案:

答案 0 :(得分:0)

您可以像普通方式一样创建通知,不需要做任何额外的事情,比如贝娄

  public class ReceiveMessage extends BroadcastReceiver {
        final SmsManager mysms = SmsManager.getDefault();
        final String ALERT="A";
        final String ALERT2="B";

        public void onReceive (Context context, Intent intent) {
            Bundle myBundle = intent.getExtras();
            try {
                if (myBundle != null) {
                    final Object [] pdus = (Object[])myBundle.get("pdus");
                    String format = myBundle.getString("format");
                    for (int i = 0; i < pdus.length ; i++) {
                        SmsMessage mynewsms= SmsMessage.createFromPdu((byte[]) pdus[i], format);
                        String sms = mynewsms.getMessageBody();
                        Toast.makeText(context, "SMS"+mynewsms, Toast.LENGTH_SHORT).show();
                        if (sms.equals(ALERT) {
                            Toast.makeText(context,"............",Toast.LENGTH_LONG).show();
                          showNotification(context,"Alert","............");
                        }
                        if (sms.equals(ALERT2) {
                            Toast.makeText(context,"Your child is out save zone",Toast.LENGTH_LONG).show();
                            showNotification(context,"Alert","Your child is out save zone");
                        }
                    }
                }
            } catch (Exception ex) {
            }
        }
    }


public static void showNotification(Context context, String title, String msg) {
        Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        Intent intent = new Intent();
        PendingIntent pIntent = PendingIntent.getActivity(context, 2, intent, PendingIntent.FLAG_ONE_SHOT);
        NotificationCompat.BigTextStyle bigTextNotiStyle = null;
        try {
            bigTextNotiStyle = new NotificationCompat.BigTextStyle();
            bigTextNotiStyle.setBigContentTitle(title);
            bigTextNotiStyle.bigText("" + msg);
        } catch (Exception e) {
            e.printStackTrace();
        }
        NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        NotificationCompat.Builder mBuilder = (NotificationCompat.Builder) new NotificationCompat.Builder(context)
                .setSmallIcon(R.drawable.ic_noti_icon)
                .setContentTitle("" + title)
                .setContentText("" + msg)
                .setStyle(bigTextNotiStyle)
                .setAutoCancel(true)
                .setColor(ContextCompat.getColor(context, R.color.staticbg))
                .setContentIntent(pIntent)
                .setLights(Color.GREEN, 3000, 3000);
        mBuilder.setVibrate(new long[]{1000, 1000, 1000, 1000, 1000});
        mBuilder.setSound(soundUri); //Sound to play
        notificationManager.notify(2, mBuilder.build());

    }