辞退正在进行Android的通知通过操作按钮不打开应用程序 [英] Dismiss Ongoing Android Notification Via Action Button Without Opening App

查看:155
本文介绍了辞退正在进行Android的通知通过操作按钮不打开应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序,有一个持续的通知,以帮助记忆。我希望能够驳回该通知的操作按钮中的一个,但我不希望打开应用程序时,按下按钮。我想preFER使用内置的通知操作按钮,而不是创建一个RemoteViews对象来填充通知。我看到一个帖子提到使用一个BroadcastReceiver该按钮被容纳在我的应用程序,虽然他的教程是非常无益的,它听起来就像这是朝着正确的方向发展。

 意图resultIntent =新的意向书(getBaseContext(),Dashboard.class);
    TaskStackBuilder stackBuilder = TaskStackBuilder.create(getBaseContext());
        stackBuilder.addParentStack(Dashboard.class);
        stackBuilder.addNextIntent(resultIntent);
        PendingIntent resultPendingIntent =
                stackBuilder.getPendingIntent(
                    0,
                    PendingIntent.FLAG_UPDATE_CURRENT
                );
    意图取消=新的意图(getBaseContext(),CancelNotification.class);
        stackBuilder.addParentStack(Dashboard.class);
        stackBuilder.addNextIntent(取消);
        PendingIntent pendingCancel =
                stackBuilder.getPendingIntent(
                    0,
                    PendingIntent.FLAG_UPDATE_CURRENT
                );

    NotificationCompat.Builder MB =新NotificationCompat.Builder(getBaseContext());
        mb.setSmallIcon(R.drawable.cross_icon);
        mb.setContentTitle(REF);
        mb.setContentText(VER);
        mb.setPriority(NotificationCompat.PRIORITY_LOW);
        mb.setOngoing(真正的);
        mb.setStyle(新NotificationCompat.BigTextStyle()bigText(VER));
        mb.setContentIntent(resultPendingIntent);
        mb.addAction(R.drawable.ic_cancel_dark,辞退,pendingCancel);

    manager.notify(1,mb.build());
 

解决方案

开始用这样的:

  INT最终NOTIFICATION_ID = 1;

//创建一个Intent的BroadcastReceiver的
意图buttonIntent =新的意图(背景下,ButtonReceiver.class);
buttonIntent.putExtra(notificationId,NOTIFICATION_ID);

//创建了PendingIntent
PendingIntent btPendingIntent = PendingIntent.getBroadcast(上下文,0,buttonIntent,0);

//通过这个PendingIntent到意向生成器的addAction方法
NotificationCompat.Builder MB =新NotificationCompat.Builder(getBaseContext());
.....
.....
.....
mb.addAction(R.drawable.ic_Action,我的行动,btPendingIntent);
manager.notify(NOTIFICATION_ID,mb.build());
 

创建BroadcastReceiver的:

 公共类ButtonReceiver扩展的BroadcastReceiver {

    @覆盖
    公共无效的onReceive(上下文的背景下,意图意图){

        INT notificationId = intent.getIntExtra(notificationId,0);

        //做你想做的人。
        ..............
        ..............

        //如果你想取消通知
        NotificationManager经理=(NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
        manager.cancel(notificationId);
    }
}
 

如果您鸵鸟政策想表现出任何活动时,用户就通知一下,定义这样的意图传中 setContentIntent

  PendingIntent resultPendingIntent = PendingIntent.getActivity(背景下,0,新的意向(),0);
......
......
mb.setContentIntent(resultPendingIntent);
 

希望这有助于!

I have an app that has an ongoing notification to help with memorization. I want to be able to dismiss this notification with one of the action button, but I don't want to open the app when the button is hit. I would prefer to use the built-in notification action buttons and not create a RemoteViews object to populate the notification. I saw one post mention using a BroadcastReceiver on this button which is received in my app, and though his tutorial was quite unhelpful, it sounds like this is headed in the right direction.

Intent resultIntent = new Intent(getBaseContext(), Dashboard.class);
    TaskStackBuilder stackBuilder = TaskStackBuilder.create(getBaseContext());
        stackBuilder.addParentStack(Dashboard.class);
        stackBuilder.addNextIntent(resultIntent);
        PendingIntent resultPendingIntent =
                stackBuilder.getPendingIntent(
                    0,
                    PendingIntent.FLAG_UPDATE_CURRENT
                );
    Intent cancel = new Intent(getBaseContext(), CancelNotification.class);
        stackBuilder.addParentStack(Dashboard.class);
        stackBuilder.addNextIntent(cancel);
        PendingIntent pendingCancel =
                stackBuilder.getPendingIntent(
                    0,
                    PendingIntent.FLAG_UPDATE_CURRENT
                );

    NotificationCompat.Builder mb = new NotificationCompat.Builder(getBaseContext());
        mb.setSmallIcon(R.drawable.cross_icon);
        mb.setContentTitle(ref);
        mb.setContentText(ver);
        mb.setPriority(NotificationCompat.PRIORITY_LOW);
        mb.setOngoing(true);
        mb.setStyle(new NotificationCompat.BigTextStyle().bigText(ver));
        mb.setContentIntent(resultPendingIntent);
        mb.addAction(R.drawable.ic_cancel_dark, "Dismiss", pendingCancel);

    manager.notify(1, mb.build());  

解决方案

Start with this:

int final NOTIFICATION_ID = 1;

//Create an Intent for the BroadcastReceiver
Intent buttonIntent = new Intent(context, ButtonReceiver.class);
buttonIntent.putExtra("notificationId",NOTIFICATION_ID);

//Create the PendingIntent
PendingIntent btPendingIntent = PendingIntent.getBroadcast(context, 0, buttonIntent,0);

//Pass this PendingIntent to addAction method of Intent Builder
NotificationCompat.Builder mb = new NotificationCompat.Builder(getBaseContext());
.....
.....
.....
mb.addAction(R.drawable.ic_Action, "My Action", btPendingIntent);
manager.notify(NOTIFICATION_ID, mb.build());  

Create the BroadcastReceiver:

public class ButtonReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {

        int notificationId = intent.getIntExtra("notificationId", 0);

        // Do what you want were.
        ..............
        ..............

        // if you want cancel notification
        NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        manager.cancel(notificationId);
    }
}  

If you don´t want show any activity when user click on notification, define the intent pass in setContentIntent in this way:

PendingIntent resultPendingIntent = PendingIntent.getActivity(context,  0, new Intent(), 0);
......
......
mb.setContentIntent(resultPendingIntent);  

Hope this helps!

这篇关于辞退正在进行Android的通知通过操作按钮不打开应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆