Skip to content
This repository has been archived by the owner on Jul 28, 2022. It is now read-only.

Commit

Permalink
OneSignal Notifications
Browse files Browse the repository at this point in the history
Signed-off-by: avi parshan <avi.pars@gmail.com>
  • Loading branch information
avipars committed Aug 27, 2018
1 parent 022404d commit aa9deb4
Show file tree
Hide file tree
Showing 13 changed files with 149 additions and 61 deletions.
Binary file modified .idea/caches/build_file_checksums.ser
Binary file not shown.
7 changes: 7 additions & 0 deletions .idea/dictionaries/avipars.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 22 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
buildscript {
repositories {
maven { url 'https://plugins.gradle.org/m2/'}
}
dependencies {
classpath 'gradle.plugin.com.onesignal:onesignal-gradle-plugin:[0.11.2, 0.99.99]'
}
}
apply plugin: 'com.onesignal.androidsdk.onesignal-gradle-plugin'

repositories {
maven { url 'https://maven.google.com' }
}
apply plugin: 'com.android.application'

android {
Expand All @@ -6,9 +19,14 @@ android {
applicationId "com.aviparshan.pazamcount"
minSdkVersion 21
targetSdkVersion 27
versionCode 1
versionName "8.26.2018"
versionCode 2
versionName "8.27.2018"
resConfigs("en", "iw")
manifestPlaceholders = [
onesignal_app_id: '09a33c2f-2829-4480-9de2-12857e4a920c',
// Project number pulled from dashboard, local value is ignored.
onesignal_google_project_number: 'REMOTE'
]
}
buildTypes {
release {
Expand All @@ -26,5 +44,7 @@ dependencies {
implementation 'net.danlew:android.joda:2.9.9.4'
implementation 'com.android.support:cardview-v7:27.1.1'
implementation 'com.startapp:inapp-sdk:3.10.1'
implementation 'com.onesignal:OneSignal:[3.9.2, 3.99.99]'


}
2 changes: 0 additions & 2 deletions app/proguard-rules.pro
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,3 @@
-keepattributes Exceptions, InnerClasses, Signature, Deprecated, SourceFile, LineNumberTable, *Annotation*, EnclosingMethod
-dontwarn android.webkit.JavascriptInterface
-dontwarn com.startapp.**

-dontwarn org.jetbrains.annotations.**
11 changes: 5 additions & 6 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.BLUETOOTH" />
<application
android:allowBackup="true"
android:name=".ApplicationClass"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:launchMode="singleTask"
android:allowBackup="false"
android:theme="@style/DarkAppTheme">
<activity
android:name=".Main"
Expand All @@ -28,23 +31,19 @@
<activity
android:name="com.startapp.android.publish.ads.list3d.List3DActivity"
android:theme="@android:style/Theme" />

<activity
android:name="com.startapp.android.publish.adsCommon.activities.OverlayActivity"
android:configChanges="orientation|keyboardHidden|screenSize"
android:theme="@android:style/Theme.Translucent" />

<activity
android:name="com.startapp.android.publish.adsCommon.activities.FullScreenActivity"
android:configChanges="orientation|keyboardHidden|screenSize"
android:theme="@android:style/Theme" />

<service android:name="com.startapp.android.publish.common.metaData.PeriodicMetaDataService" />
<service android:name="com.startapp.android.publish.common.metaData.InfoEventService" />
<service
android:name="com.startapp.android.publish.common.metaData.PeriodicJobService"
android:permission="android.permission.BIND_JOB_SERVICE" />

</application>

</manifest>
21 changes: 21 additions & 0 deletions app/src/main/java/com/aviparshan/pazamcount/ApplicationClass.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.aviparshan.pazamcount;

import android.app.Application;

import com.onesignal.OneSignal;

/**
* Created by avipars on 8/27/2018 on com.aviparshan.pazamcount
*/
public class ApplicationClass extends Application {
@Override
public void onCreate() {
super.onCreate();

// OneSignal Initialization
OneSignal.startInit(this)
.inFocusDisplaying(OneSignal.OSInFocusDisplayOption.Notification)
.unsubscribeWhenNotificationsAreDisabled(true)
.init();
}
}
6 changes: 3 additions & 3 deletions app/src/main/java/com/aviparshan/pazamcount/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
public class Main extends AppCompatActivity {

private static final String[] paths = {"2 Years 8 Months", "2 Years 6 Months", "2 Years 4 Months", "2 Years", "1 Year 6 Months", "1 Year", "6 Months"};
public static final String CHANNEL_ID = "PazamOlam Channel";
public static final String CHANNEL_ID = "PazamOlam_Channel";
private Spinner spinner;
DatePicker datePicker;
String start_date;
Expand Down Expand Up @@ -102,7 +102,7 @@ protected void onCreate(Bundle savedInstanceState) {
Date formattedDate = new Date(Helper.getLongPref(help.datePreferenceKey, getApplicationContext()));
String dayString = new SimpleDateFormat("dd", Locale.ENGLISH).format(formattedDate);
String monthString = new SimpleDateFormat("MM", Locale.ENGLISH).format(formattedDate);
String yearString = new SimpleDateFormat("YYYY", Locale.ENGLISH).format(formattedDate);
String yearString = new SimpleDateFormat("yyyy", Locale.ENGLISH).format(formattedDate);
year = Integer.parseInt(yearString);
month = Integer.parseInt(monthString) - 1; //because it starts at 0
day = Integer.parseInt(dayString);
Expand Down Expand Up @@ -175,7 +175,7 @@ void setDatePref(Date startD) {
// String formatted = new SimpleDateFormat("dd/MM/yyyy", Locale.ENGLISH).format(startD);
Helper.putPref(help.datePreferenceKey, millis, getApplicationContext());
} catch (NullPointerException npe) {
Toast.makeText(this, npe.toString() + " Please report to developer", Toast.LENGTH_SHORT).show();
Toast.makeText(this, npe.toString() + getString(R.string.error_report), Toast.LENGTH_SHORT).show();
// Calendar c = Calendar.getInstance();
// long millis = c.getTimeInMillis();
// Helper.putPref("Date", millis, getApplicationContext());
Expand Down
85 changes: 49 additions & 36 deletions app/src/main/java/com/aviparshan/pazamcount/Results.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@
import android.annotation.TargetApi;
import android.app.Activity;
import android.app.ActivityOptions;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.os.Bundle;
Expand Down Expand Up @@ -58,13 +61,20 @@ public class Results extends AppCompatActivity {
Date f;
int progress;
boolean released = false;
boolean reachedYear = false;
boolean reachedYear = false, reachedTwo = false;
int days_left;
private Handler handler = new Handler();
private Runnable runnable;
Toolbar toolbar;
Helper help = new Helper();

//used for register alarm manager
PendingIntent pendingIntent;
//used to store running alarmmanager instance
AlarmManager alarmManager;
//Callback function for Alarmmanager event
BroadcastReceiver mReceiver;

@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public static void setStatusBarGradient(Activity activity) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
Expand All @@ -91,13 +101,14 @@ Date getFutureDate(Date currentDate, int days) {
}

void setDiff() {
Resources res = getResources();
Date current_date = new Date();
int timer = Helper.getIntPref(help.spinnerPreferenceKey, getApplicationContext()); //gets results form shared prefs
int service = Helper.serviceTime(timer);
Date startD = new Date(Helper.getLongPref(help.datePreferenceKey, getApplicationContext()));
int days = (int) (service * 30.4167);
f = getFutureDate(startD, days);
DateFormat df = new SimpleDateFormat("EEEE, MMMM dd, yyyy", Locale.ENGLISH);
DateFormat df = new SimpleDateFormat("EEEE, MMMM dd, yyyy", Locale.ENGLISH); //Lowercase y to fix issue on older devices with SimpleDateFormat
DateFormat simple = new SimpleDateFormat("MM/dd/yyyy", Locale.ENGLISH);

String reportDate = df.format(f);
Expand All @@ -112,6 +123,8 @@ void setDiff() {
int served = total_days - days_left;
if (served == 365) {
reachedYear = true;
} else if (served == 730) {
reachedTwo = true;
}
double monthsLeft = (days_left) / 30.4375;
double weeksLeft = (days_left) / 7;
Expand All @@ -127,29 +140,28 @@ void setDiff() {
progress = abs(progress);
}
} catch (ArithmeticException e) {
Toast.makeText(this, "Error: Divide by zero " + e.getMessage(), Toast.LENGTH_SHORT).show();
Toast.makeText(this, getString(R.string.divide_zero) + e.getMessage(), Toast.LENGTH_SHORT).show();
progress = 0;
}
setProcessBar(progress);
if (!current_date.after(f)) //already released, because otherwise math is messed up
{
released = false;
stats.setText(total_days + " Total Days\n" + Helper.Rounder(monthsLeft) + " Months Left\n" +
weeksLeft + " Weeks Left\n" + hoursLeft + " Hours Left\n" + minutesLeft + " Minutes Left\n"
+ secondsLeft + " Seconds Left\n");
// stats.setText(total_days + " Total Days\n" + Helper.Rounder(monthsLeft) + " Months Left\n" +
// weeksLeft + " Weeks Left\n" + hoursLeft + " Hours Left\n" + minutesLeft + " Minutes Left\n"
// + secondsLeft + " Seconds Left\n");
Helper.animateTextView(0, served, pazamDays);
pazamStats.setText(Helper.Rounder(monthsDone) + " Months Served\n" + Helper.Rounder(weeksDone) + " Weeks Served\n");

String stats_view = res.getString(R.string.stats_textview, total_days, Helper.Rounder(monthsLeft), weeksLeft, hoursLeft, minutesLeft, secondsLeft);
String months_served = res.getString(R.string.time_served, Helper.Rounder(monthsDone), Helper.Rounder(weeksDone));
stats.setText(stats_view);
pazamStats.setText(months_served);
} else {
released = true;
Toast.makeText(this, "Service Finished", Toast.LENGTH_SHORT).show();
subDaysServed.setText("Service Days");
subDaysLeft.setText(" Days out of Army");
stats.setText(Helper.Rounder(abs(monthsLeft)) + " Months Out of Army\n" +
abs(weeksLeft) + " Weeks Out of Army\n" + abs(hoursLeft) + " Hours Out of Army\n" + abs(minutesLeft) + " Minutes Out of Army\n"
+ abs(secondsLeft) + " Seconds Out of Army\n");
subDaysServed.setText(R.string.service_days);
subDaysLeft.setText(R.string.days_out);
String time_out = res.getString(R.string.released_stats, Helper.Rounder(abs(monthsLeft)), abs(weeksLeft), abs(hoursLeft), abs(minutesLeft), abs(secondsLeft));
stats.setText(time_out);
Helper.animateTextView(0, total_days, pazamDays);

}

draft.setText(draftDate);
Expand All @@ -171,6 +183,8 @@ void setDiff() {
// count.setText("Days Left: " + abs(days_left));
// serv.setText("Days Served: " + served);
// progre.setText("Progress: " + progress + "%");


// }

@Override
Expand Down Expand Up @@ -226,22 +240,7 @@ protected void onCreate(Bundle savedInstanceState) {
setDiff();
if (released) {
card3.setVisibility(View.GONE);
notifyMilestones(1, "Milestone Reached", "You have finished your service, congrats! ");
}
if (progress == 50) {
notifyMilestones(2, "Milestone Reached", "You have hit the wall, congrats! ");
}
if (reachedYear) {
notifyMilestones(3, "Pazamhuledet", "You have finished a year of service, congrats! ");
}
if (progress == 50) {
notifyMilestones(2, "Milestone Reached", "You have hit the wall, congrats! ");
}
if (days_left == 1 || days_left == 0) {
notifyMilestones(2, "Almost Out", "You are almost out of the army, congrats! ");

}



// card.setOnClickListener(new View.OnClickListener() {
Expand All @@ -265,6 +264,7 @@ protected void onCreate(Bundle savedInstanceState) {
// });
}


@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
Expand All @@ -286,8 +286,7 @@ public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
// getMenuInflater().inflate(R.menu.menu_patient_home_screen, menu);

menu.add(0, 1, 1, menuIconWithText(getResources().getDrawable(R.drawable.ic_date_), getResources().getString(R.string.go_back)));

menu.add(0, 1, 1, menuIconWithText(getResources().getDrawable(R.drawable.ic_stat_onesignal_default), getResources().getString(R.string.go_back)));
return true;
}

Expand All @@ -305,9 +304,9 @@ private CharSequence menuIconWithText(Drawable r, String title) {
SpannableString sb = new SpannableString(" " + title);
ImageSpan imageSpan = new ImageSpan(r, ImageSpan.ALIGN_BOTTOM);
sb.setSpan(imageSpan, 0, 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

return sb;
}

protected void onStop() {
super.onStop();
stopUpdates();
Expand All @@ -327,8 +326,22 @@ private void stopUpdates() {
}
}

private void notifyMilestones(int notificationId, String title, String achievement) {
public void checkMilestones() {
if (released) {
notifyMilestones(1, getString(R.string.milestone), getString(R.string.finished_service));
} else if (progress == 50) {
notifyMilestones(2, getString(R.string.milestone), getString(R.string.half_service));
} else if (reachedYear) {
notifyMilestones(3, getString(R.string.birthday), getString(R.string.year_bday));
} else if (reachedTwo) {
notifyMilestones(4, getString(R.string.birthday), getString(R.string.two_year_bday));
} else if (days_left == 1 || days_left == 0) {
notifyMilestones(5, getString(R.string.almost), getString(R.string.alomst_out));

}
}

private void notifyMilestones(int notificationId, String title, String achievement) {
// Create an explicit intent for an Activity in your app
Intent intent = new Intent(this, Results.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
Expand All @@ -337,16 +350,16 @@ private void notifyMilestones(int notificationId, String title, String achieveme
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this, CHANNEL_ID)
.setSmallIcon(R.drawable.date)
.setContentTitle(title)
.setContentText(achievement + progress + "% Reached")
.setContentText(achievement + progress + "%")
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.setContentIntent(pendingIntent)
.setAutoCancel(true);
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);

// notificationId is a unique int for each notification that you must define

notificationManager.notify(notificationId, mBuilder.build());
}

}


4 changes: 3 additions & 1 deletion app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
<Spinner
android:id="@+id/spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_height="48dp"
android:layout_below="@id/serviceTime"
android:layout_marginBottom="20dp"
android:layout_marginTop="5dp" />
Expand All @@ -63,6 +63,7 @@
android:layout_height="wrap_content"
android:layout_below="@id/draftDate"
android:layout_centerInParent="true"
android:contentDescription="@string/date_picker"
android:layout_margin="@dimen/text_margin_small"
android:background="@drawable/rounded_button"
android:datePickerMode="calendar"
Expand All @@ -79,6 +80,7 @@
android:focusable="true"
android:padding="16dp"
android:src="@drawable/ic_done"
android:contentDescription="@string/done_button"
android:visibility="invisible"
app:fabSize="normal"
app:layout_anchor="@id/relative"
Expand Down
Loading

0 comments on commit aa9deb4

Please sign in to comment.