不显示通知 [英] Notifications don't appear

查看:31
本文介绍了不显示通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to develop an app that shows notifications when a new item is added.The user can select if he wants the notifications and the notification sound to show using two switch buttons. The app compiled without an error, there wasn't even an error on the stack trace but when I run the app on my phone and turn on the switches, the notification doesn't appear. What is wrong?

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);



                }

推荐答案

我认为您遇到了问题,因为通知是通过频道工作的.

I think that you have the issue because the notifications are work with a channel.

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

Check this post, it works for me

通知未显示在奥利奥中

这篇关于不显示通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆