java - Android:通知栏中未显示通知

标签 java android android-notifications android-broadcastreceiver android-notification-bar

我正在尝试在触发警报时在通知栏中显示通知。下面是我的代码。

AlarmReciever.java

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.telephony.SmsManager;
import android.util.Log;
import android.widget.Toast;

/**
 * Created by Yohan on 6/29/2016.
 */
public class AlarmReciever extends BroadcastReceiver
{
    @Override
    public void onReceive(Context context, Intent intent)
    {
        // TODO Auto-generated method stub


        // here you can start an activity or service depending on your need
        // for ex you can start an activity to vibrate phone or to ring the phone

       String message="Hi I will be there later, See You soon";// message to send



        // Show the toast  like in above screen shot
        Log.d("Alarm",message);
        Toast.makeText(context, "Alarm Triggered and SMS Sent", Toast.LENGTH_LONG).show();

        Intent intent2 = new Intent(context, NotificationReceiverActivity.class);
        PendingIntent pIntent = PendingIntent.getActivity(context, (int) System.currentTimeMillis(), intent2, 0);

        Notification noti = new Notification.Builder(context)
                .setContentTitle("New mail from " + "test@gmail.com")
                .setContentIntent(pIntent).getNotification();
        NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        // hide the notification after its selected
        noti.flags |= Notification.FLAG_AUTO_CANCEL;

        notificationManager.notify(0, noti);
    }

}

这里我使用了 getNotification() 而不是 build() 是因为我的最小 API 是 15。

NotificationReceiverActivity.java

import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;

public class NotificationReceiverActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_notification_receiver);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });
    }

}

list

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="voice1.xxx.com.alarmcheck2" >

    <uses-permission android:name="com.android.alarm.permission.SET_ALARM" />
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name"
            android:theme="@style/AppTheme.NoActionBar" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <receiver android:name=".BootCompletedReceiver" >
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
            </intent-filter>
        </receiver>
        <receiver
            android:name=".AlarmReciever"
            android:process=":remote" />

        <activity
            android:name=".NotificationReceiverActivity"
            android:label="@string/title_activity_notification_receiver"
            android:theme="@style/AppTheme.NoActionBar" >
        </activity>
    </application>

</manifest>

在我的 AlarmReciever.java 中,Toast 被触发,所以我知道它正在工作。但是通知不是。知道这是为什么吗?

最佳答案

正如文档所说,你必须放一个小图标:

Required notification contents A Notification object must contain the following:

A small icon, set by setSmallIcon() A title, set by setContentTitle() Detail text, set by setContentText()

更多详细信息请参见此处:https://developer.android.com/guide/topics/ui/notifiers/notifications.html

关于java - Android:通知栏中未显示通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38096329/

相关文章:

java - Setter 没有为 java 对象设置值

java - kotlin/java-是否有类似TryParse()的东西?

java - 如何从 Firestore 中的集合中检索所有文档?

android - OREO 和 Nougat 设备中的定期调度程序

java - cas 自定义注销

java - Android Java,如何获取类属性的值?

java - 未定义的 getTabHost() 方法

java - findViewByID 找不到 ListView

android - 通知 Intent 未启动 Activity android

flutter - 在 flutter 中如何清除栏中的通知?