Skip to content

Commit

Permalink
Merge pull request #51 from cyclexuxu/noti
Browse files Browse the repository at this point in the history
set level change notification, added emoji to message
  • Loading branch information
dyin4 authored Dec 5, 2020
2 parents 005d777 + 70bb1e7 commit 6b4400b
Show file tree
Hide file tree
Showing 14 changed files with 400 additions and 86 deletions.
6 changes: 6 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<uses-permission android:name="android.permission.WRITE_SETTINGS" ></uses-permission>
<uses-permission android:name="android.permission.CHANGE_CONFIGURATION" ></uses-permission>
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" ></uses-permission>
<uses-permission android:name="android.permission.VIBRATE" />

<application
android:allowBackup="true"
Expand All @@ -21,6 +22,11 @@
android:enabled="true"
android:exported="true" />

<service
android:name=".Test.CheckAppRunService"
android:enabled="true"
android:exported="true" />

<service
android:name=".Notifications.NotificationService"
android:enabled="true"
Expand Down
40 changes: 40 additions & 0 deletions app/src/main/java/neu/madcourse/walkwithme/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import android.app.NotificationManager;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
Expand All @@ -15,6 +18,7 @@

import com.google.android.material.bottomnavigation.BottomNavigationView;

import neu.madcourse.walkwithme.Test.CheckAppRunService;
import neu.madcourse.walkwithme.profile.ProfileActivity;
import neu.madcourse.walkwithme.profile.ProfileFragment;
import neu.madcourse.walkwithme.ranking.RankingActivity;
Expand All @@ -33,6 +37,11 @@
public class MainActivity extends AppCompatActivity {
private Button login;
FragmentTransaction fragmentTransaction;
private SharedPreferences settings;
SharedPreferences.Editor editor;
public final static String CHANNEL = "WalkWithMe";
public final static String TAG = "MainActivity";

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Expand All @@ -47,6 +56,21 @@ protected void onCreate(Bundle savedInstanceState) {
new StepsFragment2()).commit();
}

settings = getSharedPreferences(CHANNEL, MODE_PRIVATE);
editor = settings.edit();

//Is it the first time to use the app
if(!settings.contains("preRun")) {
Log.d(TAG, "This is the first time to run this app");
//enable notification
//editor.putLong("preRun", System.currentTimeMillis());
editor.putBoolean("notification", true);

Log.d(TAG, "Enable notification");
}
editor.putLong("preRun", System.currentTimeMillis());
editor.commit();

Intent startIntent = new Intent(this, StepService3.class);
startIntent.setAction(Constants.START_FOREGROUND);
startService(startIntent);
Expand Down Expand Up @@ -103,6 +127,8 @@ public void onClick(View view) {
//stopIntent.setAction(Constants.STOP_FOREGROUND);
stopService(stopIntent);
Intent login = new Intent(this, LoginActivity.class);
NotificationManager notificationManager = (NotificationManager) getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.cancel(1); //close all notification for current users
startActivity(login);
break;
case R.id.not_logout:
Expand All @@ -127,10 +153,24 @@ private void showFragment(Fragment frag) {
fragmentTransaction.replace(R.id.nav_host_fragment, frag).commit();
manager.executePendingTransactions();
}
@Override
protected void onStop() {
super.onStop();
editor.putLong("preRun", System.currentTimeMillis());
editor.commit();
Log.v(TAG, "On Stop, Starting CheckRecentRun service...");
startService(new Intent(this, CheckAppRunService.class));

}

@Override
protected void onDestroy() {
super.onDestroy();
editor.putLong("preRun", System.currentTimeMillis());
editor.commit();
Log.v(TAG, "On destory, Starting CheckRecentRun service...");
startService(new Intent(this, CheckAppRunService.class));

Intent stopIntent = new Intent(this, StepService3.class);
stopIntent.setAction(Constants.STOP_FOREGROUND);
startService(stopIntent);
Expand Down
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;
}
}

This file was deleted.

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?";

}
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());
}


}
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;
}
}
Loading

0 comments on commit 6b4400b

Please sign in to comment.