【问题标题】:How to make Notification appear on the phone for Android Studio?如何让 Android Studio 的通知出现在手机上?
【发布时间】:2019-11-12 10:56:15
【问题描述】:

我想创建一个推送通知,管理员可以在其中向所有用户发送通知。我找到了一个教程并按照它,但它不起作用。我不确定我在哪里做错了,但我得到了错误提示

Developer Warning for package "... " Failed to post notification on channel "null"

 b1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String tittle = ed1.getText().toString().trim();
                String subject = ed2.getText().toString().trim();
                String body = ed3.getText().toString().trim();

                NotificationManager notif = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
                Notification notify = new Notification.Builder
                        (getApplicationContext()).setContentTitle(tittle).setContentText(body).
                        setContentTitle(subject).setSmallIcon(R.drawable.ps).build();

                notify.flags |= Notification.FLAG_AUTO_CANCEL;
                notif.notify(0, notify);
            }
        });

【问题讨论】:

标签: java android push-notification


【解决方案1】:

在 Oreo SDK 之后,你必须创建通知通道才能显示通知,请查看此方法以供参考:

/**
 *
 * @param context
 * @param title  --> title to show
 * @param message --> details to show
 * @param intent --> What should happen on clicking the notification
 * @param reqCode --> unique code for the notification
 */

public void showNotification(Context context, String title, String message, Intent intent, int reqCode) {
    SharedPreferenceManager sharedPreferenceManager = SharedPreferenceManager.getInstance(context);

    PendingIntent pendingIntent = PendingIntent.getActivity(context, reqCode, intent, PendingIntent.FLAG_ONE_SHOT);
    String CHANNEL_ID = "channel_name";// The id of the channel.
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context, CHANNEL_ID)
            .setSmallIcon(R.mipmap.notification_logo)
            .setContentTitle(title)
            .setContentText(message)
            .setAutoCancel(true)
            .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
            .setContentIntent(pendingIntent);
    NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        CharSequence name = "Channel Name";// The user-visible name of the channel.
        int importance = NotificationManager.IMPORTANCE_HIGH;
        NotificationChannel mChannel = new NotificationChannel(CHANNEL_ID, name, importance);
        notificationManager.createNotificationChannel(mChannel);
    }
    notificationManager.notify(reqCode, notificationBuilder.build()); // 0 is the request code, it should be unique id

    Log.d("showNotification", "showNotification: " + reqCode);
}

如何使用此方法:

    int reqCode = 1;
    Intent intent = new Intent(getApplicationContext(), MainActivity.class);
    showNotification(this, "Title", "This is the message to display", intent, reqCode);

【讨论】:

  • 我必须在班级的同一页面上实现它吗?
  • 它是一个公共函数,您在参数中传递上下文,您可以使用此方法在Android系统中显示通知。
  • 我有问题。我的 onCreate 上有notification()。如何在我的 onCreate 上将(Context context, String title, String message, Intent intent, int reqCode) 传递到我的notification 中?
  • 我已经添加了实现。
  • 如果您使用 Firebase 推送通知服务,那么您可以通过传递数据显示收到的推送通知
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-04-13
  • 2022-08-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多