2

I have a notification that have an action "Open Dial". I want when I click on "Open dial" action. My notification will disappear and Dial will appear.

I tried setAutocancel(true) and set notification.flags = Notification.FLAG_AUTO_CANCEL but these not working.

I know that I can't use SetAutocancel and set Flag_Auto_Cancel when I set action for my notification. If i want do that, I must use cancel(id) function of NotificationManager.

My idea to do that is sending an IntentFilter and using BroadcastReceiver to receive it When "Open Dial" is clicked.

But I don't know how I can do that because Intent.Action_Dial is an activity. If I use PendingIntent.getBroacast(), functions in onReceive() of Broadcast will work, but Dial won't show. Anyone know how I can resolve this problem ?.

This is my notication:

  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);

this is my receiver

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");
    }
}

This is mainifest

<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>
ichthyocentaurs
  • 2,173
  • 21
  • 35
Xiao King
  • 369
  • 1
  • 13

1 Answers1

0

The solution is to start the Activity in the BroadcastReceiver, after cancelling the Notification. Here is the code for the PendingIntent:

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

and the code for the 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);
    }

Hopefully it will help someone in the future.

Chris
  • 5,876
  • 3
  • 43
  • 69
Xiao King
  • 369
  • 1
  • 13