无法实现android通知
我非常感谢您对我在Android emulator中使用通知的疑难解答提供的任何帮助。我尝试了几个不同的版本,但都没有成功,下面的代码似乎将所需的元素提取到了最低限度。问题是通知根本无法发送。当我在模拟器中点击按钮时,我听到“咔嗒”声,但没有听到其他声音 我曾在某个地方读到,错误的图像类型可能会导致问题,我已经尝试了几种。我现在包含的一个是使用res文件夹中的Vector Asset生成的。感觉上我缺少了一些简单的东西,但从我在网上找到的所有例子中可以看出,基本元素都是合理的 任何帮助或指导都将不胜感激无法实现android通知,android,notifications,Android,Notifications,我非常感谢您对我在Android emulator中使用通知的疑难解答提供的任何帮助。我尝试了几个不同的版本,但都没有成功,下面的代码似乎将所需的元素提取到了最低限度。问题是通知根本无法发送。当我在模拟器中点击按钮时,我听到“咔嗒”声,但没有听到其他声音 我曾在某个地方读到,错误的图像类型可能会导致问题,我已经尝试了几种。我现在包含的一个是使用res文件夹中的Vector Asset生成的。感觉上我缺少了一些简单的东西,但从我在网上找到的所有例子中可以看出,基本元素都是合理的 任何帮助或指导都将
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
public class MainActivity extends AppCompatActivity {
NotificationCompat.Builder notification;
private static final int idNumber = 123;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
notification = new NotificationCompat.Builder(this);
notification.setAutoCancel(true);
}
public void sendNotification(View view){
notification.setSmallIcon(R.drawable.ic_android_black_24dp);
notification.setContentText("This is the notification message");
notification.setContentTitle("Notification");
notification.setTicker("There is a notification");
notification.setWhen(System.currentTimeMillis());
Intent intent = new Intent(this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
notification.setContentIntent(pendingIntent);
NotificationManager nm = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
nm.notify(idNumber, notification.build());
}
}
大家好,欢迎来到Stack Overflow,请花点时间浏览“了解您在这里的道路”(以及赢得您的第一个徽章),阅读如何创建并检查,以增加获得反馈和有用答案的机会。您发布的通知创建代码看起来不错。你确定叫它吗?(我在代码中没有看到onclick处理程序)方法被调用(onclick是通过XML布局设置的)。当我在调试器中运行它时,我可以“单击”按钮,它就会跳转到该方法。然后,我可以在没有任何错误或其他问题的情况下运行它。它只是不产生通知。您好,欢迎来到Stack Overflow,请花点时间浏览“了解您在这里的道路(以及赢得您的第一个徽章)”,阅读如何创建并检查,以增加获得反馈和有用答案的机会。您发布的通知创建代码看起来不错。你确定叫它吗?(我在代码中没有看到onclick处理程序)方法被调用(onclick是通过XML布局设置的)。当我在调试器中运行它时,我可以“单击”按钮,它就会跳转到该方法。然后,我可以在没有任何错误或其他问题的情况下运行它。它只是不产生通知。