java - 为什么我的 android-notification 没有出现?

标签 java android

我创建了一个 android 通知,编译后的 sdk 版本为 28,最小 sdk 版本为 26,我实际上开发了需要通知的 android,所以 1st 我创建了一个与我的 FirstApplication 分开的 android 通知,它工作正常但是当我添加它时notification 在我的 FirstApplication 中,通知没有出现。

应用程序.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);
        }
    }
}

主 Activity .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);
    }
}

当我打开应用程序时,应该会出现通知,但它不起作用。所以我请求你们帮助我并检查我的代码是否遗漏了什么。

最佳答案

看看这个我想说什么:-

在你的应用标签中添加 name="App"这是你创建 channel 的类名,也是对Application类的扩展

<?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>

关于java - 为什么我的 android-notification 没有出现?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55430447/

相关文章:

android - 未找到 JNI_load..应用程序关闭并在循环中运行

android - 从 fragment 调用 fragment

android - 为我的 android 应用程序选择正确的 API 级别

java - 如何从 Firebase 存储获取 URL getDownloadURL

android - 在 EditText 焦点上抑制软键盘的出现

java - 通过用户请求创建多个数组列表(JAVA)

java - Try 和 Catch 循环

Java G1 GC 处理引用对象运行缓慢

java - 使用 Maven 使用注释 @Tag ("name_test") 过滤 JUnit 5 测试用例

java - Browser-mob Proxy 没有使用 chrome 驱动程序捕获网络流量