没有出现Android通知 [英] Android-Notification doesn't appear

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

问题描述

这是用于通过单击按钮然后转到另一个类来创建通知的代码.第二类完全是空的.我不知道为什么即使它不能转到另一堂课也没有出现.

This is a code for creating a notification by click a button and then turn to another class. The second class it totally empty. I don't know why it doesn't appear even it cannot turn to another class.

我已经检查了此文件的位置",并且通知"为打开".有人可以帮我检查一下吗?

I have already checked the "sittings" of this file and the "notification" is "on". Could anyone help me to check it?

MainActivity.class

MainActivity.class

package com.example.ww.lab;

import android.app.Notification;
import android.app.PendingIntent;
import android.content.Intent;
import android.support.v4.app.NotificationManagerCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.app.NotificationManager;
import android.support.v4.app.NotificationCompat;
import android.util.Log;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {
    private int id = 1;
    private NotificationCompat.Builder notification_builder;
    private NotificationManagerCompat notification_manager;
    Button button;

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

        button = (Button) findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                notification_manager.notify(id, notification_builder.build());
            }
        });

        Intent open_activity_intent = new Intent(this, NotificationActivity.class);
        PendingIntent pending_intent = PendingIntent
            .getActivity(this, 0, open_activity_intent, PendingIntent.FLAG_CANCEL_CURRENT);

        notification_builder = new NotificationCompat.Builder(this)
            .setSmallIcon(R.drawable.ic_launcher)
            .setContentTitle("Notification Title")
            .setContentText("Notification Body")
            .setAutoCancel(true)
            .setContentIntent(pending_intent);

        notification_manager = NotificationManagerCompat.from(this);
    }
}

activity_main.xml

activity_main.xml

 <?xml version="1.0" encoding="utf-8"?>
    <android.support.constraint.ConstraintLayout       
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.ww.lab.MainActivity">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Do it!"
        android:id="@+id/button"
        android:onClick="sendNotification"/>
    </android.support.constraint.ConstraintLayout>

推荐答案

您已经说过,编译的SDK版本为26 .您需要先设置通知的频道,然后再发布.就像

As you have stated your compiled sdk version is 26. You need to set Channel of your notification before posting it. So it will be like

NotificationManager notification_manager = (NotificationManager) this
                .getSystemService(Context.NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
    String chanel_id = "3000";
    CharSequence name = "Channel Name";
    String description = "Chanel Description";
    int importance = NotificationManager.IMPORTANCE_LOW;
    NotificationChannel mChannel = new NotificationChannel(chanel_id, name, importance);
    mChannel.setDescription(description);
    mChannel.enableLights(true);
    mChannel.setLightColor(Color.BLUE);
    notification_manager.createNotificationChannel(mChannel);
    notification_builder = new NotificationCompat.Builder(this, chanel_id)
} else {
    notification_builder = new NotificationCompat.Builder(this);
}
notification_builder.setSmallIcon(R.drawable.ic_launcher)
        .setContentTitle("Notification Title")
        .setContentText("Notification Body")
        .setAutoCancel(true)
        .setContentIntent(pending_intent);

有关更多信息,请访问开发者网站

For more please visit Developer Site

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

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