Android: 通知栏中没有显示通知

Android: Notifications are not showing in 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();
            }
        });
    }

}

清单

<?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