Почему не отображается мое android-уведомление?

#java #android

#java #Android

Вопрос:

я создаю уведомление Android с помощью скомпилированной версии sdk 28 с минимальной версией sdk 26, я на самом деле разрабатываю Android, которому требуется уведомление, поэтому сначала я создаю уведомление Android отдельно от моего первого приложения, оно работает нормально, но когда я добавляю это уведомление в свое первое приложение, уведомление не появляется.

App.java

 import android.app.Application;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.os.Build;


public class App extends Application {
    public static final String CHANNEL_1_ID = "channel1";
    public static final String CHANNEL_2_ID = "channel2";

    @Override
    public void onCreate() {
        super.onCreate();

        createNotificationChannels();
    }

    private void createNotificationChannels() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel channel1 = new NotificationChannel(
                    CHANNEL_1_ID,
                    "Channel 1",
                    NotificationManager.IMPORTANCE_HIGH
            );
            channel1.setDescription("This is Channel 1");

            NotificationChannel channel2 = new NotificationChannel(
                    CHANNEL_2_ID,
                    "Channel 2",
                    NotificationManager.IMPORTANCE_LOW
            );
            channel2.setDescription("This is Channel 2");

            NotificationManager manager = getSystemService(NotificationManager.class);
            manager.createNotificationChannel(channel1);
            manager.createNotificationChannel(channel2);
        }
    }
}
  

MainActivity.java

 import android.app.Notification;
import android.support.v4.app.NotificationCompat;
import android.support.v4.app.NotificationManagerCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;

import static com.project.cms.App.CHANNEL_1_ID;
import static com.project.cms.App.CHANNEL_2_ID;


public class MainActivity extends AppCompatActivity {
    private NotificationManagerCompat notificationManager;

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

        notificationManager = NotificationManagerCompat.from(this);
        sendchannel()
        sendchannel2()
}
    public void sendchannel(){
        Notification notification = new NotificationCompat.Builder(this, CHANNEL_1_ID)
                .setSmallIcon(R.drawable.ic_one)
                .setContentTitle("Appointment Request")
                .setContentText("ACCEPTED")
                .setPriority(NotificationCompat.PRIORITY_HIGH)
                .setCategory(NotificationCompat.CATEGORY_MESSAGE)
                .build();

        notificationManager.notify(1, notification);
    }
    public void sendchannel2(){
        Notification notification = new NotificationCompat.Builder(this, CHANNEL_2_ID)
                .setSmallIcon(R.drawable.ic_one)
                .setContentTitle("Appointment Request")
                .setContentText("DECLINED")
                .setPriority(NotificationCompat.PRIORITY_LOW)
                .build();

        notificationManager.notify(2, notification);
    }
}

  

когда я открываю приложение, уведомление должно появиться, но оно не работает. Итак, я прошу вас, ребята, помочь мне и проверить мой код, если я чего-то не хватает или что.

Комментарии:

1. я думаю, вы не добавили свой класс приложения в манифест

Ответ №1:

посмотрите на это, что я пытаюсь сказать:-

В вашем теге приложения добавьте name =»App», который является именем вашего класса для создания каналов, а также расширяет класс приложения

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

 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.SET_WALLPAPER"/>
<permission android:name="android.permission.MEDIA_CONTENT_CONTROL" />

<application
    android:name=".notification"
    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=".Main2Activity" />
    <activity android:name=".Main3Activity"/>
    <service android:name=".MyService"
        android:enabled="true"/>
    <service android:name=".Service"
        android:enabled="true"/>
    <provider
        android:authorities="${applicationId}.provider"
        android:name="android.support.v4.content.FileProvider"
        android:exported="false"
        android:grantUriPermissions="true">
        <meta-data
            android:name="android.support.FILE_PROVIDER_PATHS"
            android:resource="@xml/provider_paths"/>
    </provider>
</application>