2

This is a code for creating a notification by click a button and then turn to another class. The second class it totally empty. I don't know why it doesn't appear even it cannot turn to another class.

I have already checked the "sittings" of this file and the "notification" is "on". Could anyone help me to check it?

MainActivity.class

package com.example.ww.lab;

import android.app.Notification;
import android.app.PendingIntent;
import android.content.Intent;
import android.support.v4.app.NotificationManagerCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.app.NotificationManager;
import android.support.v4.app.NotificationCompat;
import android.util.Log;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {
    private int id = 1;
    private NotificationCompat.Builder notification_builder;
    private NotificationManagerCompat notification_manager;
    Button button;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        button = (Button) findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                notification_manager.notify(id, notification_builder.build());
            }
        });

        Intent open_activity_intent = new Intent(this, NotificationActivity.class);
        PendingIntent pending_intent = PendingIntent
            .getActivity(this, 0, open_activity_intent, PendingIntent.FLAG_CANCEL_CURRENT);

        notification_builder = new NotificationCompat.Builder(this)
            .setSmallIcon(R.drawable.ic_launcher)
            .setContentTitle("Notification Title")
            .setContentText("Notification Body")
            .setAutoCancel(true)
            .setContentIntent(pending_intent);

        notification_manager = NotificationManagerCompat.from(this);
    }
}

activity_main.xml

 <?xml version="1.0" encoding="utf-8"?>
    <android.support.constraint.ConstraintLayout       
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.ww.lab.MainActivity">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Do it!"
        android:id="@+id/button"
        android:onClick="sendNotification"/>
    </android.support.constraint.ConstraintLayout>
Sneh Pandya
  • 8,197
  • 7
  • 35
  • 50
Wendy.W
  • 25
  • 1
  • 7
  • Android Studio - ADK used: Nexus 5 API 26 - API used: API 21: Android 5.0 (Lollipop) – Wendy.W Nov 09 '17 at 02:23
  • What is your compile sdk and target sdk of app's build gradle file ? – Chinmoy Debnath Nov 09 '17 at 04:28
  • i have tested it and working file with emulator Nexus 5X API 24 (android N). – Prags Nov 09 '17 at 06:28
  • Please can you check this https://stackoverflow.com/questions/43093260/notification-not-showing-in-android-8-oreo/44534559 there is some changes with android O. – Prags Nov 09 '17 at 06:32
  • @Chinmoy Debnath: minSdkVersion 21 targetSdkVersion 26 compileSdkVersion 26 – Wendy.W Nov 09 '17 at 10:37
  • @Pragati Sing: It seems no mistake happened of the code, but I have no idea why it could not showing. Is there some mistakes happen with the API level? – Wendy.W Nov 09 '17 at 10:48
  • if you look https://stackoverflow.com/questions/43093260/notification-not-showing-in-android-8-oreo?answertab=votes#tab-top there is some setChannelId concept, can need to set setChannelId. – Prags Nov 09 '17 at 11:14

1 Answers1

9

As you have stated your compiled sdk version is 26. You need to set Channel of your notification before posting it. So it will be like

NotificationManager notification_manager = (NotificationManager) this
                .getSystemService(Context.NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
    String chanel_id = "3000";
    CharSequence name = "Channel Name";
    String description = "Chanel Description";
    int importance = NotificationManager.IMPORTANCE_LOW;
    NotificationChannel mChannel = new NotificationChannel(chanel_id, name, importance);
    mChannel.setDescription(description);
    mChannel.enableLights(true);
    mChannel.setLightColor(Color.BLUE);
    notification_manager.createNotificationChannel(mChannel);
    notification_builder = new NotificationCompat.Builder(this, chanel_id)
} else {
    notification_builder = new NotificationCompat.Builder(this);
}
notification_builder.setSmallIcon(R.drawable.ic_launcher)
        .setContentTitle("Notification Title")
        .setContentText("Notification Body")
        .setAutoCancel(true)
        .setContentIntent(pending_intent);

For more please visit Developer Site

matdev
  • 4,115
  • 6
  • 35
  • 56
Chinmoy Debnath
  • 2,814
  • 16
  • 20
  • 2
    Something is wrong in this answer because there is no createNotificationChannel() method on NotificationManagerCompat. You have to use the native NotificationManager – matdev Jan 12 '18 at 20:47