Android 中的通知不起作用

Notifications in Android don't work

我正在尝试显示我的应用程序发送的通知,但它们不起作用。在我的应用程序中,给定条件,必须显示通知。这是我正在使用的方法。

public void showNotification(){

    NotificationCompat.Builder notificacion = new NotificationCompat.Builder(MapsScreen.this);
    notificacion.setTicker("Bicis cerca")
            .setContentTitle("Bicis cerca")
            .setContentText("Bicis a menos de " + ProfileScreen.getDistance())
            .setSound(Uri.parse("android.resource://com.app.sb" + R.raw.bike));

    PendingIntent myPendingIntent;
    Intent myIntent = new Intent();
    Context myContext = getApplicationContext();

    myIntent.setClass(myContext, MapsScreen.class);
    myPendingIntent = PendingIntent.getActivity(myContext, 0, myIntent, 0);
    notificacion.setContentIntent(myPendingIntent);

    Notification n = notificacion.build();

    NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    nm.notify(1,n);

    Log.d("Status", ": entrando");
}

这个方法是在条件触发后调用的,这个方法是写在日志上的,所以是有效的。

非常感谢。

您缺少强制性参数。

setSmallIconsetContentTitlesetContentMessage为必填项。

setSmallIcon 添加到您的 NotificationBuilder,它应该可以工作。

参考:Android Developers