Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package fd.ukkix_beta;
- import android.app.Notification;
- import android.app.NotificationChannel;
- import android.app.NotificationManager;
- import android.app.PendingIntent;
- import android.app.TaskStackBuilder;
- import android.content.Context;
- import android.content.Intent;
- import android.media.AudioAttributes;
- import android.media.RingtoneManager;
- import android.net.Uri;
- import android.os.Build;
- import android.support.annotation.RequiresApi;
- import android.support.v4.app.NotificationCompat;
- import android.util.Log;
- import android.view.MenuItem;
- import com.google.gson.Gson;
- import com.google.gson.reflect.TypeToken;
- import java.lang.reflect.Type;
- import java.util.ArrayList;
- import fd.ukkix_beta.models.NotificationItem;
- import fd.ukkix_beta.models.SettingsModel;
- import fd.ukkix_beta.tools.FileWorker;
- import fd.ukkix_beta.tools.Language;
- import static android.content.Context.NOTIFICATION_SERVICE;
- public class Notifier {
- NotificationCompat.Builder notiBuild;
- FileWorker fw = new FileWorker();
- Gson gson = new Gson();
- Context cont;
- MenuItem mi;
- String TAG = "mylog: Notifier";
- Language language;
- final SettingsModel settingsModel = new SettingsModel();
- public Notifier(Context cont) {
- // Initiate settings
- language = new Language(cont);
- final String settings_string = fw.read("settings");
- if (settings_string != null){
- settingsModel.setAll(gson.fromJson(settings_string, SettingsModel.class));
- } else {
- settingsModel.setSound(true);
- settingsModel.setVibration(true);
- if (language.get().equals("ru")) settingsModel.setLanguage("ru");
- else settingsModel.setLanguage("uz");
- }
- language.change(settingsModel.getLanguage());
- // Bro, just skip those lines, I won't explain them,
- // 'cause I just copied them from stackoverflow
- // If those lines don't work, you know whom to blame
- Intent resultIntent = new Intent(cont, Notifications.class);
- TaskStackBuilder stackBuilder = TaskStackBuilder.create(cont);
- stackBuilder.addNextIntentWithParentStack(resultIntent);
- PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_ONE_SHOT);
- NotificationCompat.Builder mBuilder =
- (NotificationCompat.Builder) new NotificationCompat.Builder(cont)
- .setSmallIcon(R.drawable.ic_notification)
- .setAutoCancel(true)
- .setPriority(Notification.PRIORITY_MAX)
- .setContentIntent(resultPendingIntent);
- mBuilder.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION));
- // here's our vibration pattern, I made it myself (the only thing I did)
- long[] vibration_pattern = {500, 500, 300, 700, 2400, 600, 300, 500};
- mBuilder.setVibrate(vibration_pattern);
- this.notiBuild = mBuilder;
- this.cont = cont;
- }
- public Notifier(Context cont, MenuItem mi) {
- // Bro, just skip those lines, I won't explain them,
- // 'cause I just copied them from stackoverflow
- // If those lines don't work, you know whom to blame
- Intent resultIntent = new Intent(cont, Notifications.class);
- TaskStackBuilder stackBuilder = TaskStackBuilder.create(cont);
- stackBuilder.addNextIntentWithParentStack(resultIntent);
- PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_ONE_SHOT);
- NotificationCompat.Builder mBuilder =
- (NotificationCompat.Builder) new NotificationCompat.Builder(cont)
- .setSmallIcon(R.drawable.ic_notification)
- //.setSubText("Subdata bla blaa")
- .setAutoCancel(true)
- .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
- .setPriority(Notification.PRIORITY_MAX)
- .setContentIntent(resultPendingIntent);
- //mBuilder.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION));
- // here's our vibration pattern, I made it myself (the only thing I did)
- long[] vibration_pattern = {500, 500, 300, 700, 2400, 600, 300, 500};
- if (settingsModel.isVibration()) mBuilder.setVibrate(vibration_pattern);
- this.notiBuild = mBuilder;
- this.cont = cont;
- this.mi = mi;
- }
- // Bro, you are right in the heart of our app
- // Keep your eyes widely open
- // And please, DO NOT TOUCH ANYTHING here
- // Honestly, if you change anything, I'll stab a fork into your forehead
- // Not joking, touch => fork in your forehead
- public void push(NotificationItem ni) {
- if (shouldPush(ni)){
- String CHANNEL_ID = "1";
- //Uri soundUri = Uri.parse("android.resource://" + cont.getPackageName() + "/" + R.raw.notify);
- Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
- NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(cont, CHANNEL_ID)
- .setSmallIcon(R.drawable.ic_notification)
- .setContentTitle(ni.getTitle())
- .setContentText(ni.getData())
- .setAutoCancel(true)
- .setShowWhen(true)
- .setChannelId(CHANNEL_ID)
- .setNumber(1);// number count of new notifications
- notificationBuilder.setDefaults(Notification.DEFAULT_VIBRATE);
- notificationBuilder.setDefaults(Notification.DEFAULT_SOUND);
- //notificationBuilder.setSound(soundUri);
- Intent intent = new Intent(cont, Notifications.class);
- intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK|Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TOP);
- PendingIntent contentIntent = PendingIntent.getActivity(cont, 0,
- intent, PendingIntent.FLAG_UPDATE_CURRENT);
- notificationBuilder.setContentIntent(contentIntent);
- if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
- makeNotificationChannel(CHANNEL_ID, "Ukki Channel name", NotificationManager.IMPORTANCE_HIGH, soundUri);
- }
- NotificationManager notificationManager = (NotificationManager) cont.getSystemService(NOTIFICATION_SERVICE);
- notificationManager.notify((int) System.currentTimeMillis(), notificationBuilder.build());
- }
- }
- // For API 26+
- @RequiresApi(api = Build.VERSION_CODES.O)
- private void makeNotificationChannel(String id, String name, int importance, Uri soundUri) {
- NotificationChannel channel = new NotificationChannel(id, name, importance);
- // Shows badge in app icon
- channel.setShowBadge(true);
- AudioAttributes audioAttributes = new AudioAttributes.Builder()
- .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
- .setUsage(AudioAttributes.USAGE_ALARM)
- .build();
- channel.enableVibration(true);
- channel.setSound(soundUri, audioAttributes);
- NotificationManager notificationManager = (NotificationManager) cont.getSystemService(NOTIFICATION_SERVICE);
- notificationManager.createNotificationChannel(channel);
- }
- private boolean shouldPush(NotificationItem ni){
- String local_notifications = fw.read("notifications");
- // now we convert string to array of notification item objects
- Type listType = new TypeToken<ArrayList<NotificationItem>>() {
- }.getType();
- ArrayList<NotificationItem> nm = gson.fromJson(local_notifications, listType);
- boolean is_new = true;
- if (nm != null) {
- ArrayList<NotificationItem> nis = nm; //.getData();
- Log.d(TAG, "Comparing with locals");
- for (int i = 0; i < nis.size(); i++) { // here we are comparing each id
- if (nis.get(i).getId().equals(ni.getId())) { // if notification already exists
- is_new = false;
- }
- }
- if (is_new) { // if notification is new, we'll kick it!
- // first of all, we'll save it
- nis.add(ni);
- fw.write("notifications", gson.toJson(nis));
- Log.d(TAG, "Done");
- // Yeah! Get your new notification!
- }
- Log.d(TAG, "Comparing completed");
- } else {
- Log.d(TAG, "Creating new notifications file");
- ArrayList<NotificationItem> nis_new = new ArrayList<>();
- nis_new.add(ni);
- fw.write("notifications", gson.toJson(nis_new));
- Log.d(TAG, "Done");
- }
- if (is_new) {
- String unread_string = fw.read("unread");
- ArrayList<String> unread = new ArrayList<>();
- if (unread_string != null) {
- Type notiType = new TypeToken<ArrayList<String>>() {
- }.getType();
- unread = gson.fromJson(unread_string, notiType);
- }
- unread.add(ni.getId());
- fw.write("unread", gson.toJson(unread));
- }
- return is_new;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement