Fixed notification on Android O

This commit is contained in:
PeratX 2018-06-22 20:04:17 +08:00
parent 27b50054c1
commit 50cd639c59

View File

@ -2,6 +2,7 @@ package org.itxtech.daedalus.service;
import android.annotation.TargetApi; import android.annotation.TargetApi;
import android.app.Notification; import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager; import android.app.NotificationManager;
import android.app.PendingIntent; import android.app.PendingIntent;
import android.content.Context; import android.content.Context;
@ -46,6 +47,8 @@ public class DaedalusVpnService extends VpnService implements Runnable {
private static final int NOTIFICATION_ACTIVATED = 0; private static final int NOTIFICATION_ACTIVATED = 0;
private static final String TAG = "DaedalusVpnService"; private static final String TAG = "DaedalusVpnService";
private static final String CHANNEL_ID = "daedalus_channel_1";
private static final String CHANNEL_NAME = "daedalus_channel";
public static String primaryServer; public static String primaryServer;
public static String secondaryServer; public static String secondaryServer;
@ -83,10 +86,21 @@ public class DaedalusVpnService extends VpnService implements Runnable {
NotificationManager manager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE); NotificationManager manager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this); NotificationCompat.Builder builder;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(CHANNEL_ID, CHANNEL_NAME, NotificationManager.IMPORTANCE_LOW);
manager.createNotificationChannel(channel);
builder = new NotificationCompat.Builder(this, CHANNEL_ID);
} else {
builder = new NotificationCompat.Builder(this);
}
Intent nIntent = new Intent(this, MainActivity.class); Intent deactivateIntent = new Intent(StatusBarBroadcastReceiver.STATUS_BAR_BTN_DEACTIVATE_CLICK_ACTION);
PendingIntent pIntent = PendingIntent.getActivity(this, 0, nIntent, PendingIntent.FLAG_UPDATE_CURRENT); deactivateIntent.setClass(this, StatusBarBroadcastReceiver.class);
Intent settingsIntent = new Intent(StatusBarBroadcastReceiver.STATUS_BAR_BTN_SETTINGS_CLICK_ACTION);
settingsIntent.setClass(this, StatusBarBroadcastReceiver.class);
PendingIntent pIntent = PendingIntent.getActivity(this, 0,
new Intent(this, MainActivity.class), PendingIntent.FLAG_UPDATE_CURRENT);
builder.setWhen(0) builder.setWhen(0)
.setContentTitle(getResources().getString(R.string.notice_activated)) .setContentTitle(getResources().getString(R.string.notice_activated))
.setDefaults(NotificationCompat.DEFAULT_LIGHTS) .setDefaults(NotificationCompat.DEFAULT_LIGHTS)
@ -98,10 +112,10 @@ public class DaedalusVpnService extends VpnService implements Runnable {
.setContentIntent(pIntent) .setContentIntent(pIntent)
.addAction(R.drawable.ic_clear, getResources().getString(R.string.button_text_deactivate), .addAction(R.drawable.ic_clear, getResources().getString(R.string.button_text_deactivate),
PendingIntent.getBroadcast(this, 0, PendingIntent.getBroadcast(this, 0,
new Intent(StatusBarBroadcastReceiver.STATUS_BAR_BTN_DEACTIVATE_CLICK_ACTION), 0)) deactivateIntent, PendingIntent.FLAG_UPDATE_CURRENT))
.addAction(R.drawable.ic_settings, getResources().getString(R.string.action_settings), .addAction(R.drawable.ic_settings, getResources().getString(R.string.action_settings),
PendingIntent.getBroadcast(this, 0, PendingIntent.getBroadcast(this, 0,
new Intent(StatusBarBroadcastReceiver.STATUS_BAR_BTN_SETTINGS_CLICK_ACTION), 0)); settingsIntent, PendingIntent.FLAG_UPDATE_CURRENT));
Notification notification = builder.build(); Notification notification = builder.build();