1

I have a really simple Service which triggers a notification. But it does not work. As you probably see, all of these are just copied from the net, but I want it to work, before I tweak it.

This is called from within the service:

NotificationManager mNotificationManager =
        (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

String id = "my_channel_01";
CharSequence name = "someName";
String description = "even bigger name";
int importance = NotificationManager.IMPORTANCE_HIGH;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
    NotificationChannel mChannel = new NotificationChannel(id, name, importance);
    mChannel.setDescription(description);
    mChannel.enableLights(true);
    mChannel.setLightColor(Color.RED);
    mChannel.enableVibration(true);
    mChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
    mNotificationManager.createNotificationChannel(mChannel);
}

NotificationCompat.Builder mBuilder =
        new NotificationCompat.Builder(getApplicationContext())
                .setSmallIcon(R.drawable.big_anchor)
                .setContentTitle("My notification")
                .setChannel(id)
                .setContentText("Hello World!");
Intent resultIntent = new Intent(getApplicationContext(), MainActivity.class);

PendingIntent resultPendingIntent =
        PendingIntent.getActivity(getApplicationContext(),
                0, resultIntent,
                PendingIntent.FLAG_UPDATE_CURRENT
        );
mBuilder.setContentIntent(resultPendingIntent);

mNotificationManager.notify(32345, mBuilder.build());

I've read, that some parameters should be placed in AndroidManifest.xml so also I am attaching this:

<application
    android:name=".App"
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name"
        android:theme="@style/AppTheme.NoActionBar">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <service android:name=".GpsService" />
</application>

Also, the only logs I'm receiving are:

10-30 22:44:13.939 417-417/asdf.appW/Notification: Use of stream types is deprecated for operations other than volume control
10-30 22:44:13.940 417-417/asdf.app W/Notification: See the documentation of setSound() for what to use instead with android.media.AudioAttributes to qualify your playback use case

Any ideas?

UPDATE

I suppose this is something related to the API version I am using. When I used API 24 everything is working. But when using Emulator with 26, nothing appears.

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Michał
  • 616
  • 1
  • 7
  • 22

2 Answers2

2

You have to provide the channel ID in the notification builder constructor:

new NotificationCompat.Builder(getApplicationContext(), id)

Reference

Juan Cruz Soler
  • 8,172
  • 5
  • 41
  • 44
  • In what version of AppCompact is it? I've got `com.android.support:appcompat-v7:26.0.0-alpha1` and the mentioned constructor does not exist. – Michał Nov 01 '17 at 09:35
  • 1
    Ok, got it. I've had to use a google maven repository. https://stackoverflow.com/questions/44500176/setting-up-gradle-for-api-26-android – Michał Nov 01 '17 at 10:05
-1

I also faced similar issue, NotificationCompat.Builder was not working for me for API 26. Try using Notification.Builder for API 26 and NotificationCompat.Builder for versions lower than 26.

user2622786
  • 779
  • 3
  • 9
  • 21