java - 不出现通知

标签 java android notifications switch-statement

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

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;
import android.support.v4.app.NotificationManagerCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.Switch;

import java.util.Set;

import androidx.annotation.RequiresApi;

import static android.app.PendingIntent.getActivity;
import static android.content.Context.NOTIFICATION_SERVICE;
import static com.example.myevents.R.drawable.notification;


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() {
            @TargetApi(Build.VERSION_CODES.O)
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked){{
                    int notifyID = 1;
                    String CHANNEL_ID = "my_channel_01";// The id of the channel.
                    CharSequence name = "channel 1";// The user-visible name of the channel.
                    int importance = NotificationManager.IMPORTANCE_HIGH;
                    NotificationChannel mChannel = new NotificationChannel(CHANNEL_ID, name, importance);

                    Intent intent = new Intent(Settings.this, Visitor.class);
                    intent.putExtra("yourpackage.notifyId", notifyID);
                    PendingIntent pIntent = PendingIntent.getActivity(Settings.this, 0, intent,
                            PendingIntent.FLAG_UPDATE_CURRENT);


// Create a notification and set the notification channel.
                    Notification notification =
                            new NotificationCompat.Builder(Settings.this)
                                    .setSmallIcon(R.drawable.notification)
                                    .setContentTitle("My Events")
                                    .setContentText("Νέα εκδήλωση κοντά σας!")
                                    .setChannelId(CHANNEL_ID).build();



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

// Issue the notification.
                    mNotificationManager.notify(notifyID , notification);



                }

最佳答案

我认为您遇到此问题是因为通知与 channel 一起使用。

查看这篇文章,它对我有用

Notification not showing in Oreo

关于java - 不出现通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60919140/

相关文章:

Java - map.get 不起作用,但元素在 map 中?

java - 在并行测试执行中使用 Hashmap 作为线程安全

Java:检查 JButton 是否在 Frame MousePressListener 内按下

java - 在调用方法之前检查非空实际值时出现 NullPointerException

java - 还有其他人在使用 Java 8 和 Android Studio 3.0 时遇到问题吗?

android - gradle 3.2.1 的数据绑定(bind)问题

notifications - 完成工作后如何在终端中获得声音通知

java - 在特定日期和时间创建警报

android - setAndAllowWhileIdle 和 setExactAndAllowWhileIdle 的区别

ios - Firebase FCM主题-可以是已订阅用户的用户ID吗?