【问题标题】:Creating a notification with a pending intent创建具有待处理意图的通知
【发布时间】:2017-11-06 02:07:30
【问题描述】:

所以我对 android 还很陌生,我正在尝试让一个按钮创建一个通知,然后单击此通知拨打电话。我在网上做了很多环顾四周,但我仍然无法弄清楚为什么这不起作用。我有这个方法。

   public void notification(){
    Intent callIntent = new Intent(Intent.ACTION_CALL);
    callIntent.setData(Uri.parse("tel: 0210000001"));
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, callIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    android.support.v4.app.NotificationCompat.Builder mBuilder =
            new NotificationCompat.Builder(this)
                    .setSmallIcon(R.drawable.ic_stat_name)
                    .setContentTitle("My notification")
                    .setContentText("Click to ring 021 000 0001")
                    .setContentIntent(pendingIntent);
}

以及调用该方法的这个按钮。

Button notif = (Button) findViewById(R.id.notificationButton);
    notif.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            notification();
        }
    });}

任何帮助将不胜感激。

【问题讨论】:

标签: android android-intent notifications


【解决方案1】:

您需要从您的NotificationBuilder 构建通知,然后通知。

Notification mNotification = mBuilder.build();
mNotification.notify();

NotificationManager mManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mManager.notify(1234, mNotification);

数字1234 是一个随机的任意数字,用于从您的进程中识别此通知。您可以输入任何数字而不必担心,除非稍后您想操作此特定通知(例如取消它)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多