13 package ai.picovoice.porcupinedemoservice;
15 import android.app.Notification;
16 import android.app.NotificationChannel;
17 import android.app.NotificationManager;
18 import android.app.PendingIntent;
19 import android.app.Service;
20 import android.content.Context;
21 import android.content.Intent;
22 import android.os.Build;
23 import android.os.IBinder;
24 import android.util.Log;
26 import androidx.annotation.Nullable;
27 import androidx.core.app.NotificationCompat;
40 private static final String
CHANNEL_ID =
"PorcupineServiceChannel";
41 private static final String
ACCESS_KEY =
"${YOUR_ACCESS_KEY_HERE}";
48 final String contentText =
numUtterances == 1 ?
" time!" :
" times!";
53 NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
54 assert notificationManager !=
null;
55 notificationManager.notify(1234, n);
59 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
60 NotificationChannel notificationChannel =
new NotificationChannel(
63 NotificationManager.IMPORTANCE_HIGH);
65 NotificationManager manager = getSystemService(NotificationManager.class);
66 manager.createNotificationChannel(notificationChannel);
80 .setSensitivity(0.7
f).build(
81 getApplicationContext(),
87 String.format(
"%s\nEnsure your accessKey '%s' is a valid access key.", e.getMessage(),
ACCESS_KEY)
102 getNotification(
"Porcupine init failed",
"Service will be shut down") :
104 startForeground(1234, notification);
106 return super.onStartCommand(intent, flags, startId);
110 Intent i =
new Intent(
"PorcupineInitError");
111 i.putExtra(
"errorMessage", message);
116 PendingIntent pendingIntent = PendingIntent.getActivity(
122 return new NotificationCompat.Builder(
this,
CHANNEL_ID)
123 .setContentTitle(title)
124 .setContentText(message)
125 .setSmallIcon(
R.drawable.ic_launcher_foreground)
126 .setContentIntent(pendingIntent)
143 Log.e(
"PORCUPINE", e.toString());