-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #51 from cyclexuxu/noti
set level change notification, added emoji to message
- Loading branch information
Showing
14 changed files
with
400 additions
and
86 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
107 changes: 107 additions & 0 deletions
107
app/src/main/java/neu/madcourse/walkwithme/Test/CheckAppRunService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
package neu.madcourse.walkwithme.Test; | ||
|
||
import android.app.AlarmManager; | ||
import android.app.Notification; | ||
import android.app.NotificationChannel; | ||
import android.app.NotificationManager; | ||
import android.app.PendingIntent; | ||
import android.app.Service; | ||
import android.content.Context; | ||
import android.content.Intent; | ||
import android.content.SharedPreferences; | ||
import android.os.IBinder; | ||
import android.util.Log; | ||
|
||
import androidx.core.app.NotificationCompat; | ||
|
||
import neu.madcourse.walkwithme.MainActivity; | ||
import neu.madcourse.walkwithme.R; | ||
|
||
public class CheckAppRunService extends Service { | ||
//If user hasn't use the app for 3 days, send notification | ||
|
||
private final static String TAG = "CheckRecentPlay"; | ||
private static Long MILLISECS_PER_DAY = 86400000L; | ||
private static Long MILLISECS_PER_MIN = 60000L; | ||
private static String CHANNEL_ID = "WalkWithMe"; | ||
|
||
//private static long delay = MILLISECS_PER_MIN ; // 30s (for testing) | ||
private static long delay = MILLISECS_PER_DAY * 3; // 3 days | ||
|
||
@Override | ||
public void onCreate() { | ||
super.onCreate(); | ||
|
||
Log.d(TAG, "Service started"); | ||
SharedPreferences settings = getSharedPreferences(MainActivity.CHANNEL, MODE_PRIVATE); | ||
|
||
// Are notifications enabled? | ||
if (settings.getBoolean("notification", true)) { | ||
// Is it time for a notification? | ||
if (settings.getLong("preRun", Long.MAX_VALUE) < System.currentTimeMillis() - delay) | ||
sendNotification(); | ||
|
||
} else { | ||
Log.i(TAG, "Notifications are disabled"); | ||
} | ||
|
||
// Set an alarm for the next time this service should run: | ||
setAlarm(); | ||
|
||
Log.v(TAG, "Service stopped"); | ||
stopSelf(); | ||
} | ||
|
||
public void setAlarm() { | ||
|
||
Intent serviceIntent = new Intent(this, CheckAppRunService.class); | ||
PendingIntent pi = PendingIntent.getService(this, 131313, serviceIntent, | ||
PendingIntent.FLAG_CANCEL_CURRENT); | ||
|
||
AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE); | ||
am.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + delay, pi); | ||
Log.v(TAG, "Alarm set"); | ||
Log.v(TAG, System.currentTimeMillis() + delay + ""); | ||
} | ||
|
||
public void sendNotification() { | ||
|
||
Intent intent = new Intent(this , MainActivity.class); | ||
|
||
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); | ||
|
||
PendingIntent resultPendingIntent = PendingIntent.getActivity(this, | ||
0 /* Request code */, intent, | ||
PendingIntent.FLAG_UPDATE_CURRENT); | ||
|
||
|
||
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this, CHANNEL_ID); | ||
mBuilder.setSmallIcon(R.drawable.happy); | ||
mBuilder.setContentTitle("We miss you") | ||
.setContentText("Content") | ||
.setAutoCancel(true) | ||
.setContentIntent(resultPendingIntent); | ||
|
||
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); | ||
|
||
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) | ||
{ | ||
|
||
NotificationChannel serviceChannel = new NotificationChannel( | ||
CHANNEL_ID, | ||
"WALKWITHME", | ||
NotificationManager.IMPORTANCE_HIGH | ||
); | ||
mNotificationManager.createNotificationChannel(serviceChannel); | ||
} | ||
assert mNotificationManager != null; | ||
mNotificationManager.notify(0 /* Request Code */, mBuilder.build()); | ||
|
||
Log.v(TAG, "Notification sent"); | ||
} | ||
|
||
@Override | ||
public IBinder onBind(Intent intent) { | ||
return null; | ||
} | ||
} |
7 changes: 0 additions & 7 deletions
7
app/src/main/java/neu/madcourse/walkwithme/Test/NofiticationCenter.java
This file was deleted.
Oops, something went wrong.
23 changes: 23 additions & 0 deletions
23
app/src/main/java/neu/madcourse/walkwithme/Test/NofiticationConstants.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package neu.madcourse.walkwithme.Test; | ||
|
||
|
||
import neu.madcourse.walkwithme.R; | ||
|
||
public class NofiticationConstants{ | ||
public static final NotificationMessage L2 = new NotificationMessage("Congratulations \uD83C\uDF8A \uD83C\uDF8A","You've leveled up to level 2!", 1, R.drawable.fireworks); | ||
public static final NotificationMessage L3 = new NotificationMessage("Hooray \uD83C\uDF89 \uD83C\uDF89", "That's level 3!", 2, R.drawable.fireworks); | ||
public static final NotificationMessage L4 = new NotificationMessage("Woo Hoo \uD83D\uDC4F \uD83D\uDC4F \uD83D\uDC4F", "You made it to level 4!", 3, R.drawable.fireworks); | ||
public static final NotificationMessage L5 = new NotificationMessage("❤Amazing❤", "You've reached level 5!", 4, R.drawable.fireworks); | ||
|
||
|
||
|
||
|
||
public static final String[] LEVEL_3 = {"Hooray! That's level 3!", "1"}; | ||
public static final String[] LEVEL_4 = {"Woo Hoo! You made it to level 4!", "1"}; | ||
public static final String[] LEVEL_5 = {"Amazing!! You've reached level 5!", "1"}; | ||
|
||
// NotifiContst.L2.msg | ||
public static final String REACH_DAILY_GOAL = "Excellent! You have met your daily goal!"; | ||
public static final String THREE_DAYS_INACTIVE = "Uh oh, We miss you! Why don't you check what is going in your app?"; | ||
|
||
} |
91 changes: 91 additions & 0 deletions
91
app/src/main/java/neu/madcourse/walkwithme/Test/NotificationCenter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
package neu.madcourse.walkwithme.Test; | ||
|
||
import android.app.Notification; | ||
import android.app.NotificationChannel; | ||
import android.app.NotificationManager; | ||
import android.app.PendingIntent; | ||
import android.content.Context; | ||
import android.content.Intent; | ||
import android.graphics.Bitmap; | ||
import android.graphics.BitmapFactory; | ||
|
||
import androidx.core.app.NotificationCompat; | ||
|
||
import neu.madcourse.walkwithme.MainActivity; | ||
import neu.madcourse.walkwithme.R; | ||
|
||
public class NotificationCenter { | ||
|
||
private Context context; | ||
private static final String CHANNEL_ID = "WalkWithMe"; | ||
//NotificationManager manager = null; | ||
|
||
public NotificationCenter(Context context) { | ||
this.context = context; | ||
} | ||
// | ||
// private void createNotificationChannel() { | ||
// if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) { | ||
// NotificationChannel serviceChannel = new NotificationChannel( | ||
// CHANNEL_ID, | ||
// "WALKWITHME", | ||
// NotificationManager.IMPORTANCE_HIGH | ||
// ); | ||
// manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); | ||
// manager.createNotificationChannel(serviceChannel); | ||
// } | ||
// } | ||
// private Notification getNotification(String title, String body, int id){ | ||
// Intent intent = new Intent(context, MainActivity.class); | ||
// PendingIntent resultPendingIntent = PendingIntent.getActivity(context,id,intent,PendingIntent.FLAG_ONE_SHOT); | ||
// Notification notification = new NotificationCompat.Builder(context,CHANNEL_ID) | ||
// .setContentTitle(title) | ||
// .setContentText(body) | ||
// .setLargeIcon(Bitmap.createScaledBitmap(BitmapFactory.decodeResource(context.getResources(), R.drawable.happy),97,128,false)) | ||
// .setSmallIcon(R.drawable.happy) | ||
// .setContentIntent(resultPendingIntent) | ||
// .setOngoing(true) | ||
// .setAutoCancel(true) | ||
// .build(); | ||
// | ||
// return notification; | ||
// } | ||
|
||
public void createNotification(NotificationMessage notificationMessage) | ||
{ | ||
|
||
Intent intent = new Intent(context , MainActivity.class); | ||
|
||
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); | ||
|
||
PendingIntent resultPendingIntent = PendingIntent.getActivity(context, | ||
0 /* Request code */, intent, | ||
PendingIntent.FLAG_UPDATE_CURRENT); | ||
|
||
|
||
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context, CHANNEL_ID); | ||
mBuilder.setSmallIcon(R.mipmap.ic_launcher_walkwithme); | ||
mBuilder.setContentTitle(notificationMessage.title) | ||
.setContentText(notificationMessage.body) | ||
.setLargeIcon(Bitmap.createScaledBitmap(BitmapFactory.decodeResource(context.getResources(), NofiticationConstants.L2.imgSrc),110,110,false)) | ||
.setAutoCancel(true) | ||
.setContentIntent(resultPendingIntent); | ||
|
||
NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); | ||
|
||
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) | ||
{ | ||
|
||
NotificationChannel serviceChannel = new NotificationChannel( | ||
CHANNEL_ID, | ||
"WALKWITHME", | ||
NotificationManager.IMPORTANCE_HIGH | ||
); | ||
mNotificationManager.createNotificationChannel(serviceChannel); | ||
} | ||
|
||
mNotificationManager.notify(notificationMessage.msgId /* Request Code */, mBuilder.build()); | ||
} | ||
|
||
|
||
} |
15 changes: 15 additions & 0 deletions
15
app/src/main/java/neu/madcourse/walkwithme/Test/NotificationMessage.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package neu.madcourse.walkwithme.Test; | ||
|
||
public class NotificationMessage { | ||
public String title = ""; | ||
public String body = ""; | ||
public int msgId = 0; | ||
public int imgSrc = 0; | ||
|
||
public NotificationMessage(String title, String body, int msgId, int imgSrc) { | ||
this.title= title; | ||
this.body= body; | ||
this.msgId = msgId; | ||
this.imgSrc = imgSrc; | ||
} | ||
} |
Oops, something went wrong.