Implement notification channel

Этот коммит содержится в:
Anton Tananaev
2017-08-30 15:27:00 +12:00
родитель ab37fd478a
Коммит 8d94b8d011
3 изменённых файлов: 28 добавлений и 27 удалений
+23 -1
Просмотреть файл
@@ -1,5 +1,5 @@
/*
* Copyright 2016 Anton Tananaev (anton.tananaev@gmail.com)
* Copyright 2016 - 2017 Anton Tananaev (anton.tananaev@gmail.com)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -15,14 +15,36 @@
*/
package org.traccar.client;
import android.annotation.TargetApi;
import android.app.Application;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.Context;
import android.graphics.Color;
import android.os.Build;
public class MainApplication extends Application {
public static final String PRIMARY_CHANNEL = "default";
@Override
public void onCreate() {
super.onCreate();
System.setProperty("http.keepAliveDuration", String.valueOf(30 * 60 * 1000));
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
registerChannel();
}
}
@TargetApi(Build.VERSION_CODES.O)
private void registerChannel() {
NotificationChannel channel = new NotificationChannel(
PRIMARY_CHANNEL, getString(R.string.channel_default), NotificationManager.IMPORTANCE_MIN);
channel.setLightColor(Color.GREEN);
channel.setLockscreenVisibility(Notification.VISIBILITY_SECRET);
((NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE)).createNotificationChannel(channel);
}
}
+4 -26
Просмотреть файл
@@ -1,5 +1,5 @@
/*
* Copyright 2012 - 2015 Anton Tananaev (anton.tananaev@gmail.com)
* Copyright 2012 - 2017 Anton Tananaev (anton.tananaev@gmail.com)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -23,11 +23,9 @@ import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.IBinder;
import android.support.v4.app.NotificationCompat;
import android.util.Log;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class TrackingService extends Service {
private static final String TAG = TrackingService.class.getSimpleName();
@@ -37,32 +35,12 @@ public class TrackingService extends Service {
private static Notification createNotification(Context context) {
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, new Intent(context, MainActivity.class), 0);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
return new Notification.Builder(context)
return new NotificationCompat.Builder(context, MainApplication.PRIMARY_CHANNEL)
.setContentTitle(context.getString(R.string.app_name))
.setContentText(context.getString(R.string.settings_status_on_summary))
.setContentIntent(pendingIntent)
.setPriority(Notification.PRIORITY_MIN)
.setPriority(NotificationCompat.PRIORITY_MIN)
.build();
} else {
Notification notification = new Notification(android.R.drawable.stat_notify_sync_noanim, null, 0);
try {
Method method = notification.getClass().getMethod("setLatestEventInfo", Context.class, CharSequence.class, CharSequence.class, PendingIntent.class);
try {
method.invoke(notification, context, context.getString(R.string.app_name), context.getString(R.string.settings_status_on_summary), pendingIntent);
} catch (IllegalArgumentException | IllegalAccessException | InvocationTargetException e) {
Log.w(TAG, e);
}
} catch (SecurityException | NoSuchMethodException e) {
Log.w(TAG, e);
}
return notification;
}
}
@TargetApi(Build.VERSION_CODES.ECLAIR)
+1
Просмотреть файл
@@ -5,6 +5,7 @@
<string name="shortcut_start">Traccar - Start</string>
<string name="shortcut_stop">Traccar - Stop</string>
<string name="shortcut_sos">Traccar - SOS</string>
<string name="channel_default">Primary Channel</string>
<string name="settings_id_title">Device identifier</string>
<string name="settings_url_title">Server URL</string>
<string name="settings_url_summary">Tracking server URL</string>