Skip to content
This repository has been archived by the owner on Feb 8, 2021. It is now read-only.

Commit

Permalink
Merge pull request #3 from crtlib/master
Browse files Browse the repository at this point in the history
Create notification channel on Oreo and higher
  • Loading branch information
crtlib authored Jul 12, 2018
2 parents cf18ee3 + 866a867 commit e9d94ac
Showing 1 changed file with 31 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,22 @@
package com.m2049r.xmrwallet.service;

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.os.Binder;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Looper;
import android.os.Message;
import android.os.Process;
import android.support.annotation.RequiresApi;
import android.support.v4.app.NotificationCompat;

import com.m2049r.xmrwallet.R;
import com.m2049r.xmrwallet.WalletActivity;
Expand All @@ -37,12 +43,15 @@
import com.m2049r.xmrwallet.model.WalletManager;
import com.m2049r.xmrwallet.util.Helper;

import junit.framework.Assert;

import timber.log.Timber;

public class WalletService extends Service {
public static boolean Running = false;

final static int NOTIFICATION_ID = 2049;
final static String NOTIFICATION_CHANNEL_ID = "loki_wallet_notification";

public static final String REQUEST_WALLET = "wallet";
public static final String REQUEST = "request";
Expand Down Expand Up @@ -548,11 +557,32 @@ private Wallet openWallet(String walletName, String walletPassword) {
private void startNotification() {
Intent notificationIntent = new Intent(this, WalletActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
Notification notification = new Notification.Builder(this)

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
createNotificationChannelIfNeeded();
}
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID);
Notification notification = builder
.setContentTitle(getString(R.string.service_description))
.setSmallIcon(R.drawable.loki_notification)
.setContentIntent(pendingIntent)
.build();
startForeground(NOTIFICATION_ID, notification);
}

@RequiresApi(Build.VERSION_CODES.O)
private void createNotificationChannelIfNeeded() {
NotificationManager service = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
Assert.assertTrue(service != null);

if (service.getNotificationChannel(NOTIFICATION_CHANNEL_ID) != null) {
return;
}
NotificationChannel chan = new NotificationChannel(NOTIFICATION_CHANNEL_ID,
getString(R.string.service_description), NotificationManager.IMPORTANCE_LOW);
chan.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
chan.enableLights(false);
chan.enableVibration(false);
service.createNotificationChannel(chan);
}
}

0 comments on commit e9d94ac

Please sign in to comment.