AQEEL AHMED
AQEEL AHMED

Reputation: 13

ANDROID O Notification does not work.

using

import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.support.v4.app.TaskStackBuilder;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;
import android.view.View;

created 2nd activity called secoundactivity
on 1st Activity created button and calling following method

        String CHANNEL_ID = "my_channel_01";
    NotificationCompat.Builder mBuilder =
            new NotificationCompat.Builder(this)
                    .setSmallIcon(R.drawable.network_service)
                    .setContentTitle("My notification")
                    .setContentText("Hello World!")
            .setChannel(CHANNEL_ID);

                    //.setchannelid(CHANNEL_ID);

    Intent resultIntent = new Intent(this, secondactivity.class);

    TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
    stackBuilder.addParentStack(secondactivity.class);
    stackBuilder.addNextIntent(resultIntent);
    PendingIntent resultPendingIntent =
            stackBuilder.getPendingIntent(
                    0,
                    PendingIntent.FLAG_UPDATE_CURRENT
            );
    mBuilder.setContentIntent(resultPendingIntent);
    NotificationManager mNotificationManager =
            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

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

and using
Compile SDK Version : API 26: Android 8.0 (O)
Build Tool : 26.0.1

Please Help as i am just new

Upvotes: 0

Views: 826

Answers (2)

AQEEL AHMED
AQEEL AHMED

Reputation: 13

Things you need to know

.1 Notification .2 Notification.Builder .3 NotificationCompat.Builder

following Solution work for me.

Created 2 Activity

.1 "MainActivity"
.2 "SecondClass" (please ignore the name conv)

Created one button in MainActivity and calling method getSendNotification(View vm)

public void getSendNotification(View vm) {
    String id = "1";
    CharSequence name = "my Channel";
    String description = "Description";
    int importance = NotificationManager.IMPORTANCE_LOW;
    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});

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


    Notification.Builder  mBuilder
    = new Notification.Builder(getApplicationContext())
            .setSmallIcon(R.drawable.preferences_desktop_notification_bell)
            .setContentTitle("Title")
            .setContentText("Text")
            .setChannelId(id);

    Intent resultIntent = new Intent(this, SecondClass.class);
    TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
    stackBuilder.addParentStack(SecondClass.class);
    stackBuilder.addNextIntent(resultIntent);
    PendingIntent resultPendingIntent =stackBuilder.getPendingIntent(0,PendingIntent.FLAG_UPDATE_CURRENT);
    mBuilder.setContentIntent(resultPendingIntent);
    mNotificationManager.notify(1, mBuilder.build());

}


AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>

<application
    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">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <activity android:name=".SecondClass">
        <intent-filter>
            <action android:name="second_filter" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>


</application>


but Notification.Builder (i have message is deprecated , mean i should not use it )

but with NotificationCompat.Builder

i am still receiving error message

failed to post notification on Channel "null"


only working with Notification.Builder

Upvotes: -2

advice
advice

Reputation: 5988

You're setting the channel in the notification, but you need to create the channel through NotificationManager first.

So, first create the channel like this.

val manager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
// Creating a channel - required for O's notifications.
val channel = NotificationChannel("my_channel_01",
         "Channel human readable title",
         NotificationManager.IMPORTANCE_DEFAULT)
manager.createNotificationChannel(channel)

And then, you can use the channel's id when building your notification.

// Building the notification.
val builder = Notification.Builder(context, channel.id)
// Using the channel's id in the builder.
builder.setChannelId(channel.id)

And finally, you can post your notification.

// Posting the notification.
manager.notify(notificationId, builder.build())

Upvotes: 5

Related Questions