未显示通知

问题描述 投票:0回答:1

我正在尝试开发一个在添加新项目时显示通知的应用程序。用户可以选择是否要使用两个开关按钮显示通知和通知声音。该应用程序编译时没有错误,堆栈跟踪上甚至没有错误,但是当我在手机上运行该应用程序并打开开关时,不会出现通知。怎么了?

import android.annotation.TargetApi;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.Switch;

import androidx.annotation.RequiresApi;

import static android.app.PendingIntent.getActivity;
import static android.content.Context.NOTIFICATION_SERVICE;


public class Settings extends AppCompatActivity {
    Switch simpleswitch1;
    Switch simpleswitch2;
    private Notification notification;
    NotificationManager manager;
    Notification myNotication;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_settings);


        simpleswitch1 = (Switch) findViewById(R.id.simpleswitch1);
        simpleswitch2 = (Switch) findViewById(R.id.simpleswitch2);
        simpleswitch1.setChecked(false);
        simpleswitch2.setChecked(false);
        simpleswitch1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked){{
                    Intent intent = new Intent("com.rj.notitfications.SECACTIVITY");

                    PendingIntent pendingIntent = PendingIntent.getActivity(Settings.this, 1, intent, 0);

                    Notification.Builder builder = new Notification.Builder(Settings.this);

                    builder.setAutoCancel(false);
                    builder.setTicker("this is ticker text");
                    builder.setContentTitle("WhatsApp Notification");
                    builder.setContentText("You have a new message");
                    builder.setSmallIcon(R.drawable.notification);
                    builder.setContentIntent(pendingIntent);
                    builder.setOngoing(true);
                    builder.setSubText("This is subtext...");
                    builder.setNumber(100);
                    builder.build();

                    myNotication = builder.getNotification();
                    manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
                    manager.notify(11, myNotication);
            }


        }
                simpleswitch2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                    @Override
                    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                        if (isChecked){
                            Notification.Builder builder = new Notification.Builder(Settings.this);
                            builder.setDefaults(Notification.DEFAULT_SOUND);





                }}






    });}});}}
java android notifications switch-statement
1个回答
1
投票

我认为您遇到了问题,因为通知适用于某个频道。

查看此帖子,它对我有用

Notification not showing in Oreo

© www.soinside.com 2019 - 2024. All rights reserved.