Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Intent intent= new Intent(this, NotificationListener.getClass()); startService(intent);
- public class NotificationListener extends Service {
- private SharedPreferences sharedPreferences;
- private SharedPreferences.Editor editor;
- public NotificationListener() {
- }
- public static final int JOB_ID = 0x01;
- @Override
- public IBinder onBind(Intent intent) {
- throw new UnsupportedOperationException("Not yet implemented");
- }
- @Override
- public int onStartCommand(Intent intent, int flags, int startId) {
- sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
- editor = sharedPreferences.edit();
- final Handler handler = new Handler();
- handler.postDelayed(new Runnable() {
- @Override
- public void run() {
- Post post = (Post) new Post().execute("тут url", "user_id=" + sharedPreferences.getInt("userId",0));
- try {
- ShowNotification(post.get());
- } catch (ExecutionException e) {
- Log.i("MSG","SOMETHING ERROR " + e);
- } catch (InterruptedException e) {
- Log.i("MSG","SOMETHING ERROR " + e);
- }
- handler.postDelayed(this, 10000);
- }
- }, 0);
- return START_STICKY;
- }
- private void ShowNotification(String result) {
- JSONObject dataJsonObj = null;
- try {
- dataJsonObj = new JSONObject(result);
- if (dataJsonObj.has("notification")) {
- JSONArray getNotify = dataJsonObj.getJSONArray("notification");
- String content = "";
- for (int i = 0; i < getNotify.length(); i++) {
- JSONObject notify = getNotify.getJSONObject(i);
- content += notify.getString("content");
- }
- Intent notifyIntent = new Intent(this, MainActivity.class);
- notifyIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
- PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notifyIntent, PendingIntent.FLAG_UPDATE_CURRENT);
- NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
- //builder.setContentIntent(pendingIntent);
- builder.setContentTitle("У вас новый заказ!");
- NotificationCompat.BigTextStyle bigTextStyle = new NotificationCompat.BigTextStyle();
- bigTextStyle.setBigContentTitle("У вас новый заказ!");
- bigTextStyle.bigText(content);
- builder.setContentText(content);
- builder.setStyle(bigTextStyle);
- builder.setAutoCancel(true);
- builder.setDefaults(Notification.DEFAULT_SOUND);
- builder.setWhen(System.currentTimeMillis());
- builder.setSmallIcon(R.mipmap.ic_launcher);
- builder.setContentIntent(pendingIntent);
- Bitmap largeIconBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.logo);
- builder.setLargeIcon(largeIconBitmap);
- builder.setFullScreenIntent(pendingIntent, true);
- Notification notification = builder.build();
- NotificationManager notificationManager =
- (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
- notificationManager.notify(1, notification);
- startForeground(1, notification);
- }
- }
- catch (JSONException e) {
- e.printStackTrace();
- }
- }
- }
- <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
- <uses-permission android:name="android.permission.QUICKBOOT_POWERON" />
- <uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS" />
- <service android:name=".NotificationListener"></service>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement