Added notification channel and code to show notification.

This commit is contained in:
Kartikey Kushwaha 2018-08-12 15:01:50 +05:30
parent 5e2aa51627
commit f85e19c75d
3 changed files with 64 additions and 4 deletions

View File

@ -63,6 +63,7 @@ import io.reactivex.plugins.RxJavaPlugins;
public class App extends Application {
protected static final String TAG = App.class.toString();
private RefWatcher refWatcher;
private static App context;
@SuppressWarnings("unchecked")
private static final Class<? extends ReportSenderFactory>[] reportSenderFactoryClasses = new Class[]{AcraReportSenderFactory.class};
@ -85,6 +86,8 @@ public class App extends Application {
}
refWatcher = installLeakCanary();
context = this;
// Initialize settings first because others inits can use its values
SettingsActivity.initSettings(this);
@ -202,6 +205,22 @@ public class App extends Application {
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.createNotificationChannel(mChannel);
// Set up notification channel for app update
final String appUpdateId
= getString(R.string.app_update_notification_channel_id);
final CharSequence appUpdateName
= getString(R.string.app_update_notification_channel_name);
final String appUpdateDescription
= getString(R.string.app_update_notification_channel_description);
NotificationChannel appUpdateChannel
= new NotificationChannel(appUpdateId, appUpdateName, importance);
appUpdateChannel.setDescription(appUpdateDescription);
NotificationManager appUpdateNotificationManager
= (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
appUpdateNotificationManager.createNotificationChannel(appUpdateChannel);
}
@Nullable
@ -217,4 +236,8 @@ public class App extends Application {
protected boolean isDisposedRxExceptionsReported() {
return false;
}
public static App getContext() {
return context;
}
}

View File

@ -1,7 +1,12 @@
package org.schabi.newpipe;
import android.app.Application;
import android.app.PendingIntent;
import android.content.Intent;
import android.net.Uri;
import android.os.AsyncTask;
import android.util.Log;
import android.support.v4.app.NotificationCompat;
import android.support.v4.app.NotificationManagerCompat;
import org.json.JSONException;
import org.json.JSONObject;
@ -88,8 +93,6 @@ public class FetchAppVersionTask extends AsyncTask<Void, Void, String> {
if (output != null) {
Log.i("output---", output);
try {
JSONObject mainObject = new JSONObject(output);
JSONObject flavoursObject = mainObject.getJSONObject("flavors");
@ -108,11 +111,37 @@ public class FetchAppVersionTask extends AsyncTask<Void, Void, String> {
}
}
/**
* Method to compare
* @param versionName
* @param apkLocationUrl
*/
private void compareAppVersionAndShowNotification(String versionName, String apkLocationUrl) {
if (!BuildConfig.VERSION_NAME.equals(versionName)) {
int NOTIFICATION_ID = 2000;
if (!BuildConfig.VERSION_NAME.equals(versionName.replace("v", ""))) {
Application app = App.getContext();
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(apkLocationUrl));
PendingIntent pendingIntent
= PendingIntent.getActivity(app, 0, intent, 0);
NotificationCompat.Builder notificationBuilder = new NotificationCompat
.Builder(app, app.getString(R.string.app_update_notification_channel_id))
.setSmallIcon(R.drawable.ic_newpipe_triangle_white)
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
.setContentIntent(pendingIntent)
.setAutoCancel(true)
.setContentTitle(app.getString(R.string.app_update_notification_content_title))
.setContentText(app.getString(R.string.app_update_notification_content_text)
+ " " + versionName);
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(app);
// notificationId is a unique int for each notification that you must define
notificationManager.notify(NOTIFICATION_ID, notificationBuilder.build());
}
}
}

View File

@ -144,6 +144,10 @@
<string name="notification_channel_name">NewPipe Notification</string>
<string name="notification_channel_description">Notifications for NewPipe Background and Popup Players</string>
<string name="app_update_notification_channel_id" translatable="false">newpipeAppUpdate</string>
<string name="app_update_notification_channel_name">App Update Notification</string>
<string name="app_update_notification_channel_description">Notifications for new NewPipe version</string>
<string name="unknown_content">[Unknown]</string>
<string name="toggle_orientation">Toggle Orientation</string>
@ -504,4 +508,8 @@
<item>144p</item>
</string-array>
<!-- App update notification -->
<string name="app_update_notification_content_title">NewPipe Update Available</string>
<string name="app_update_notification_content_text">Tap to download</string>
</resources>