Guest User

Untitled

a guest
Nov 14th, 2018
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.93 KB | None | 0 0
  1. class SmsReciever: BroadcastReceiver() {
  2. override fun onReceive(context: Context, intent: Intent) {
  3. val extras = intent.extras
  4.  
  5. if(extras != null) {
  6. val sms = extras.get("pdus") as Array<Any>
  7.  
  8. for(i in sms.indices){
  9. val format = extras.getString("format")
  10.  
  11. var smsMessage = if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){
  12. SmsMessage.createFromPdu(sms[i] as ByteArray, format)
  13.  
  14. } else {
  15. SmsMessage.createFromPdu(sms[i] as ByteArray)
  16.  
  17. }
  18.  
  19. val phoneNumber = smsMessage.originatingAddress
  20. val message = smsMessage.messageBody.toString()
  21.  
  22. Toast.makeText(context, "phoneNumber: $phoneNumber" + "message: $message", Toast.LENGTH_SHORT). show()
  23.  
  24. createWebNotification(context, phoneNumber, message)
  25.  
  26. val preference: SharedPreferences = PreferenceManager.getDefaultSharedPreferences(context)
  27.  
  28.  
  29. if(preference.getBoolean("auto_reply", false)){
  30. val sms = SmsManager.getDefault()
  31.  
  32. sms.sendTextMessage(phoneNumber, null, "This is an automatic Message", null, null)
  33.  
  34.  
  35. }
  36.  
  37.  
  38. }
  39.  
  40. }
  41.  
  42. }
  43.  
  44. fun createWebNotification(context: Context,title: String, text: String) {
  45. val channelId = context.getString(edu.uw.amjadz.yama.R.string.default_notification)
  46. val notificationBuilder = NotificationCompat.Builder(context, channelId)
  47. notificationBuilder.setAutoCancel(true)
  48. notificationBuilder.setSmallIcon(edu.uw.amjadz.yama.R.drawable.notification_icon_background)
  49. notificationBuilder.setContentTitle(title)
  50. notificationBuilder.setContentText(text)
  51. notificationBuilder.setDefaults(Notification.DEFAULT_SOUND)
  52.  
  53.  
  54. val mNotificationManager =
  55. context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
  56.  
  57. // pending implicit intent to view url
  58. val intent: Intent = Intent(context, MainActivity::class.java)
  59. intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
  60.  
  61.  
  62. val pending = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT)
  63. notificationBuilder.setContentIntent(pending)
  64. // Since android Oreo notification channel is needed.
  65. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
  66. val channel = NotificationChannel(
  67. channelId,
  68. "Aware CloudAlert",
  69. NotificationManager.IMPORTANCE_DEFAULT
  70. )
  71. mNotificationManager.createNotificationChannel(channel)
  72. }
  73. // using the same tag and Id causes the new notification to replace an existing one
  74. val random = Random()
  75. mNotificationManager.notify(
  76. System.currentTimeMillis().toString(),
  77. random.nextInt(),
  78. notificationBuilder.build()
  79. )
  80. }
  81.  
  82.  
  83. }
Add Comment
Please, Sign In to add comment