简体   繁体   English

Android中的通知不起作用

[英]Notifications in Android don't work

i'm trying to show notifications sent by my app, but they don't work. 我正在尝试显示我的应用发送的通知,但它们不起作用。 In my app, given a condition, the notification must be shown. 在我的应用中,有条件的话,必须显示通知。 This is the method I'm using. 这是我正在使用的方法。

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

This method is called after the condition is triggered, and the mehtod works because it writes on the log. 触发条件后,将调用此方法,该方法有效,因为它写入日志。

Thanks so much. 非常感谢。

You are missing a compulsory argument. 您缺少必修课。

setSmallIcon , setContentTitle , setContentMessage are compulsory. setSmallIconsetContentTitlesetContentMessage是强制性的。

Add setSmallIcon to your NotificationBuilder and it should work. setSmallIcon添加到您的NotificationBuilder ,它应该可以工作。

Reference: Android Developers 参考: Android开发人员

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM