Advertisement
Guest User

Untitled

a guest
Apr 26th, 2018
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.29 KB | None | 0 0
  1. package com.example.filip.unibook;
  2.  
  3. import android.app.Activity;
  4. import android.app.NotificationChannel;
  5. import android.app.NotificationManager;
  6. import android.app.PendingIntent;
  7. import android.content.Context;
  8. import android.content.Intent;
  9. import android.os.Build;
  10. import android.support.v4.app.NotificationCompat;
  11. import android.support.v4.app.NotificationManagerCompat;
  12.  
  13. /**
  14. * Created by Ludvig on 2018-03-14.
  15. */
  16.  
  17. public final class Notification{
  18.  
  19. public String CHANNEL_ID = "1";
  20. Context context;
  21. public NotificationCompat.Builder mBuilder;
  22. public NotificationManagerCompat notificationManagerCompat;
  23.  
  24.  
  25. public Notification(Context c, String program, String text) {
  26. context = c;
  27.  
  28. //Intent för att öppna UniBook om man trycker på notifikationen.
  29. Intent notifyIntent = new Intent(context, LoggedInActivity.class);
  30. notifyIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
  31. | Intent.FLAG_ACTIVITY_CLEAR_TASK);
  32. PendingIntent notifyPendingIntent = PendingIntent.getActivity(
  33. context, 0, notifyIntent, PendingIntent.FLAG_UPDATE_CURRENT
  34. );
  35.  
  36. setChannel(c);
  37.  
  38. //Bygger notifikationen
  39. mBuilder = new NotificationCompat.Builder(context, CHANNEL_ID)
  40. .setSmallIcon(R.drawable.schjool)
  41. .setContentTitle(program)
  42. .setContentText(text)
  43. .setContentIntent(notifyPendingIntent)
  44. .setPriority(NotificationCompat.PRIORITY_DEFAULT);
  45.  
  46. notificationManagerCompat = NotificationManagerCompat.from(context);
  47. }
  48.  
  49. public void setChannel(Context c){
  50. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
  51. CharSequence name = "UniBook";
  52. String description = "Bok har lagts upp";
  53. //int importance = NotificationManagerCompat.IMPORTANCE_DEFAULT;
  54. NotificationChannel channel = new NotificationChannel(CHANNEL_ID, name, NotificationManager.IMPORTANCE_HIGH);
  55. channel.setDescription(description);
  56. // Register the channel with the system
  57. NotificationManager notificationManager = c.getSystemService(NotificationManager.class);
  58. notificationManager.createNotificationChannel(channel);
  59. }
  60. }
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement