Advertisement
Guest User

Untitled

a guest
Jun 25th, 2019
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.96 KB | None | 0 0
  1. Intent intent= new Intent(this, NotificationListener.getClass()); startService(intent);
  2.  
  3. public class NotificationListener extends Service {
  4.  
  5. private SharedPreferences sharedPreferences;
  6. private SharedPreferences.Editor editor;
  7.  
  8. public NotificationListener() {
  9. }
  10.  
  11. public static final int JOB_ID = 0x01;
  12.  
  13.  
  14. @Override
  15. public IBinder onBind(Intent intent) {
  16. throw new UnsupportedOperationException("Not yet implemented");
  17. }
  18.  
  19.  
  20. @Override
  21. public int onStartCommand(Intent intent, int flags, int startId) {
  22. sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
  23. editor = sharedPreferences.edit();
  24. final Handler handler = new Handler();
  25. handler.postDelayed(new Runnable() {
  26. @Override
  27. public void run() {
  28. Post post = (Post) new Post().execute("тут url", "user_id=" + sharedPreferences.getInt("userId",0));
  29. try {
  30. ShowNotification(post.get());
  31. } catch (ExecutionException e) {
  32. Log.i("MSG","SOMETHING ERROR " + e);
  33. } catch (InterruptedException e) {
  34. Log.i("MSG","SOMETHING ERROR " + e);
  35. }
  36. handler.postDelayed(this, 10000);
  37. }
  38. }, 0);
  39. return START_STICKY;
  40. }
  41.  
  42.  
  43.  
  44. private void ShowNotification(String result) {
  45.  
  46. JSONObject dataJsonObj = null;
  47.  
  48. try {
  49. dataJsonObj = new JSONObject(result);
  50. if (dataJsonObj.has("notification")) {
  51. JSONArray getNotify = dataJsonObj.getJSONArray("notification");
  52. String content = "";
  53.  
  54. for (int i = 0; i < getNotify.length(); i++) {
  55. JSONObject notify = getNotify.getJSONObject(i);
  56. content += notify.getString("content");
  57.  
  58. }
  59. Intent notifyIntent = new Intent(this, MainActivity.class);
  60.  
  61. notifyIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
  62. PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notifyIntent, PendingIntent.FLAG_UPDATE_CURRENT);
  63.  
  64. NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
  65.  
  66. //builder.setContentIntent(pendingIntent);
  67. builder.setContentTitle("У вас новый заказ!");
  68. NotificationCompat.BigTextStyle bigTextStyle = new NotificationCompat.BigTextStyle();
  69. bigTextStyle.setBigContentTitle("У вас новый заказ!");
  70. bigTextStyle.bigText(content);
  71. builder.setContentText(content);
  72. builder.setStyle(bigTextStyle);
  73. builder.setAutoCancel(true);
  74. builder.setDefaults(Notification.DEFAULT_SOUND);
  75. builder.setWhen(System.currentTimeMillis());
  76. builder.setSmallIcon(R.mipmap.ic_launcher);
  77. builder.setContentIntent(pendingIntent);
  78. Bitmap largeIconBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.logo);
  79. builder.setLargeIcon(largeIconBitmap);
  80. builder.setFullScreenIntent(pendingIntent, true);
  81.  
  82. Notification notification = builder.build();
  83. NotificationManager notificationManager =
  84. (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
  85. notificationManager.notify(1, notification);
  86. startForeground(1, notification);
  87. }
  88. }
  89. catch (JSONException e) {
  90. e.printStackTrace();
  91. }
  92. }
  93.  
  94.  
  95.  
  96.  
  97. }
  98.  
  99. <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
  100. <uses-permission android:name="android.permission.QUICKBOOT_POWERON" />
  101. <uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS" />
  102. <service android:name=".NotificationListener"></service>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement