单击操作时如何取消通知

时间:2016-08-13 08:28:19

标签: android android-intent push-notification notifications android-notifications

我的通知中有“打开拨号”操作。我想点击“打开拨号”动作。我的通知将消失,并显示拨号。

我尝试了setAutocancel(true)并设置了notification.flags = Notification.FLAG_AUTO_CANCEL,但这些都无效。

我知道当我为通知设置操作时,我无法使用SetAutocancel并设置Flag_Auto_Cancel。如果我想这样做,我必须使用cancel(id) NotificationManager函数。

我的想法是发送IntentFilter并使用BroadcastReceiver接收它时单击“打开拨号”。

但我不知道如何做到这一点,因为Intent.Action_Dial是一项活动。如果我使用PendingIntent.getBroacast(),广播onReceive()中的功能将起作用,但Dial不会显示。有谁知道如何解决这个问题?

这是我的通知:

  NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
    builder.setContentTitle("Notification");
    builder.setContentText("This is notification");
    builder.setSmallIcon(R.mipmap.ic_launcher);
    builder.setTicker("This is ticker");
    Intent intent = new Intent(Intent.ACTION_DIAL);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, MSG_B, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    builder.addAction(R.mipmap.ic_launcher, "Dial", pendingIntent);
    builder.setAutoCancel(true);
    Notification notification = builder.build();
    notification.flags =  Notification.FLAG_AUTO_CANCEL;
    NotificationManager manager = (NotificationManager)this.getSystemService(NOTIFICATION_SERVICE);
    manager.notify(id, notification);

这是我的接收器

NotificationCompat.Builder builder;
Notification notification;
NotificationManager manager;
@Override
public void onReceive(Context context, Intent intent) {
    manager =  (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    if(intent.getAction().equals("download")){
        Log.d("LOG","Download");
        showNotify(3, context);
        DownloadFileFromURL downloadFileFromURL = new DownloadFileFromURL();
        downloadFileFromURL.execute("http://freebigpictures.com/wp-content/uploads/2009/09/river-edge.jpg");
    }

    else if(intent.getAction().equals("android.intent.action.DIAL")){
        Log.d("LOG", "cacel Dial");
        manager.cancel(NotificationServices.MSG_B);
    }

    else if(intent.getAction().equals("cancelActivity")){
        Log.d("LOG","cacel Activity");
        manager.cancel(NotificationServices.MSG_B);
    }
    else {
        Log.d("LOG","Fail ABC");
    }
}

这是主要的

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity
        android:name=".activity.MainActivity"
        android:label="@string/app_name"
        android:theme="@style/AppTheme.NoActionBar">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <service
        android:name=".services.NotificationServices"
        android:enabled="true"
        android:exported="true">
    </service>
    <receiver android:name=".broadcast.BroadcastReceiverImage">

    <intent-filter>
        <action android:name="download"/>
        <action android:name="cancelDial"/>
        <action android:name="cancelActivity"/>
        <action android:name="android.intent.action.DIAL"></action>
    </intent-filter>
    </receiver>
</application>

1 个答案:

答案 0 :(得分:0)

解决方案是在取消通知后启动BroadcastReceiver中的Activity。以下是PendingIntent:

的代码
Intent intent = new Intent();
intent.setAction("cancelDial");
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, MSG_B, intent, PendingIntent.FLAG_UPDATE_CURRENT);

和BroadcastReceiver的代码:

    else if(intent.getAction().equals("cancelDial")){
        Log.d("LOG", "Cancel Dial");
        Intent intent = new Intent(Intent.ACTION_DIAL);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(intent);
        manager.cancel(NotificationServices.MSG_B);
    }

希望将来可以帮助某人。