Skip to content

Commit

Permalink
Merge pull request #1 from skkshoaib/ticket-fix-notification
Browse files Browse the repository at this point in the history
Added channelName for Android Oreo + added support for Bluetooth plugged and unplugged device
  • Loading branch information
skkshoaib authored Dec 20, 2018
2 parents 3dd46fb + 6fac106 commit c1446b9
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 27 deletions.
2 changes: 2 additions & 0 deletions plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@

<config-file target="AndroidManifest.xml" parent="/*">
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
<uses-permission android:name="android.permission.BLUETOOTH"/>
</config-file>

<config-file target="AndroidManifest.xml" parent="/manifest/application">
Expand Down
28 changes: 12 additions & 16 deletions src/android/MusicControls.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,19 @@
import org.apache.cordova.CordovaInterface;
import org.apache.cordova.CordovaPlugin;
import org.apache.cordova.CordovaWebView;
import org.apache.cordova.PluginResult;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.util.Log;
import android.bluetooth.BluetoothDevice;
import android.app.Activity;
import android.content.Context;
import android.content.IntentFilter;
import android.content.Intent;
import android.app.PendingIntent;
import android.content.ServiceConnection;
import android.content.ComponentName;
import android.app.Service;
import android.os.IBinder;
import android.os.Bundle;
import android.os.Build;
import android.R;
import android.content.BroadcastReceiver;
import android.media.AudioManager;

public class MusicControls extends CordovaPlugin {
Expand All @@ -36,26 +30,28 @@ public class MusicControls extends CordovaPlugin {

private void registerBroadcaster(MusicControlsBroadcastReceiver mMessageReceiver){
final Context context = this.cordova.getActivity().getApplicationContext();
context.registerReceiver((BroadcastReceiver)mMessageReceiver, new IntentFilter("music-controls-previous"));
context.registerReceiver((BroadcastReceiver)mMessageReceiver, new IntentFilter("music-controls-pause"));
context.registerReceiver((BroadcastReceiver)mMessageReceiver, new IntentFilter("music-controls-play"));
context.registerReceiver((BroadcastReceiver)mMessageReceiver, new IntentFilter("music-controls-next"));
context.registerReceiver((BroadcastReceiver)mMessageReceiver, new IntentFilter("music-controls-media-button"));
context.registerReceiver((BroadcastReceiver)mMessageReceiver, new IntentFilter("music-controls-destroy"));
context.registerReceiver(mMessageReceiver, new IntentFilter("music-controls-previous"));
context.registerReceiver(mMessageReceiver, new IntentFilter("music-controls-pause"));
context.registerReceiver(mMessageReceiver, new IntentFilter("music-controls-play"));
context.registerReceiver(mMessageReceiver, new IntentFilter("music-controls-next"));
context.registerReceiver(mMessageReceiver, new IntentFilter("music-controls-media-button"));
context.registerReceiver(mMessageReceiver, new IntentFilter("music-controls-destroy"));

// Listen for headset plug/unplug
context.registerReceiver((BroadcastReceiver)mMessageReceiver, new IntentFilter(Intent.ACTION_HEADSET_PLUG));
context.registerReceiver(mMessageReceiver, new IntentFilter(Intent.ACTION_HEADSET_PLUG));
context.registerReceiver(mMessageReceiver, new IntentFilter(BluetoothDevice.ACTION_ACL_CONNECTED));
context.registerReceiver(mMessageReceiver, new IntentFilter(BluetoothDevice.ACTION_ACL_DISCONNECTED));
}

// Register pendingIntent for broacast
public void registerMediaButtonEvent(){
if (this.mediaButtonAccess && android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR2){
if (this.mediaButtonAccess){
this.mAudioManager.registerMediaButtonEventReceiver(this.mediaButtonPendingIntent);
}
}

public void unregisterMediaButtonEvent(){
if (this.mediaButtonAccess && android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR2){
if (this.mediaButtonAccess){
this.mAudioManager.unregisterMediaButtonEventReceiver(this.mediaButtonPendingIntent);
}
}
Expand Down
16 changes: 11 additions & 5 deletions src/android/MusicControlsBroadcastReceiver.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
package com.homerours.musiccontrols;

import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaPlugin;

import android.util.Log;
import android.app.Activity;
import android.bluetooth.BluetoothDevice;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.content.BroadcastReceiver;
import android.view.KeyEvent;

Expand Down Expand Up @@ -54,7 +51,16 @@ public void onReceive(Context context, Intent intent) {
default:
break;
}
} else if (message.equals("music-controls-media-button")){
}
else if(message.equals(BluetoothDevice.ACTION_ACL_CONNECTED)){
this.cb.success("{\"message\": \"music-controls-bluetooth-device-connected\"}");
this.cb = null;
}
else if(message.equals(BluetoothDevice.ACTION_ACL_DISCONNECTED)){
this.cb.success("{\"message\": \"music-controls-bluetooth-device-disconnected\"}");
this.cb = null;
}
else if (message.equals("music-controls-media-button")){
// Media button
KeyEvent event = (KeyEvent) intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT);
if (event.getAction() == KeyEvent.ACTION_DOWN) {
Expand Down
33 changes: 27 additions & 6 deletions src/android/MusicControlsNotification.java
Original file line number Diff line number Diff line change
@@ -1,25 +1,20 @@
package com.homerours.musiccontrols;

import org.apache.cordova.CordovaInterface;


import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.File;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Random;

import android.util.Log;
import android.R;
import android.app.NotificationChannel;
import android.content.Context;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.os.Build;
import android.graphics.BitmapFactory;
import android.graphics.Bitmap;
Expand All @@ -32,13 +27,15 @@ public class MusicControlsNotification {
private int notificationID;
private MusicControlsInfos infos;
private Bitmap bitmapCover;
private static final String CHANNEL_ID="memorize_quran_important_notifications";

// Public Constructor
public MusicControlsNotification(Activity cordovaActivity,int id){
this.notificationID = id;
this.cordovaActivity = cordovaActivity;
Context context = cordovaActivity;
this.notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
this.createNotificationChannel(context);
}

// Show or update notification
Expand Down Expand Up @@ -230,9 +227,33 @@ private void createBuilder(){
}
builder.setStyle(new Notification.MediaStyle().setShowActionsInCompactView(args));
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
builder.setChannelId(this.createNotificationChannel(context));
}
this.notificationBuilder = builder;
}
private String createNotificationChannel(Context context) {

// NotificationChannels are required for Notifications on O (API 26) and above.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {

// Initializes NotificationChannel.
NotificationChannel notificationChannel = new NotificationChannel(CHANNEL_ID,
"Playback notifications", NotificationManager.IMPORTANCE_LOW);
notificationChannel.setDescription("Notifications during playback ");
notificationChannel.enableVibration(false);
notificationChannel.setLockscreenVisibility(1);

// Adds NotificationChannel to system. Attempting to create an existing notification
// channel with its original values performs no operation, so it's safe to perform the
// below sequence.
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
assert notificationManager != null;
notificationManager.createNotificationChannel(notificationChannel);
return CHANNEL_ID;
}
return null;
}
private int getResourceId(String name, int fallback){
try{
if(name.isEmpty()){
Expand Down

0 comments on commit c1446b9

Please sign in to comment.