Rahulkapil
Rahulkapil

Reputation: 294

sending an audio file via push notification in android

Is it possible to send an audio file as a push notification in android ? What i want to do is that the user can record his voice as a message and then that message should be delivered to all the users with that app as a push notification . is it possible ?

Upvotes: 2

Views: 2591

Answers (3)

user756706
user756706

Reputation:

According to below document, CGM / C2DM / Push Notification can send only 4KB data, So, you can not send audio files via push notification,

http://developer.android.com/guide/google/gcm/c2dm.html "Apps can use "messages with payload" to deliver messages of up to 4 Kb"

But you can send http url of any audio file, in mobile app will receive audio file link via cgm message and download audio file using http connection.

Upvotes: 4

kirubha sankar
kirubha sankar

Reputation: 160

There is a Way, we can send a audio url in data from FCM. After that we can parse and sent to another activity.

In FIREBASE SERVICE

JSONObject object = null;
            try {
                String data = remoteMessage.getData().toString();
                Map<String, String> params = remoteMessage.getData();
                object = new JSONObject(params);

            } catch (Exception e) {
                Log.e("FCM err 1", e.getMessage());
            }

sendNotification(this, remoteMessage.getNotification().getBody(), remoteMessage.getNotification().getTitle(), remoteMessage.getNotification().getClickAction(), object);

PUSH NOTIFICATION FUNCTION

public static void sendNotification(Context context, String Message, String Title, String ClickAction, JSONObject object) {

    NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

    final String CHANNEL_ID = "KRB";
    if (Build.VERSION.SDK_INT >= 26) {  // Build.VERSION_CODES.O
        NotificationChannel mChannel = new NotificationChannel(CHANNEL_ID, "KRB_channel", NotificationManager.IMPORTANCE_HIGH);
        notificationManager.createNotificationChannel(mChannel);
    }

    // intent = Home
    Intent intent = new Intent(ClickAction);
    try {
        Log.e("FCMurl", object.getString("url"));
        Log.e("FCMtype", object.getString("type"));
        intent.putExtra("url", object.getString("url"));
        intent.putExtra("type", object.getString("type"));

        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

    } catch (Exception e) {
        //   Toast.makeText(context,  e.getMessage(), Toast.LENGTH_SHORT).show();
        Log.e("FCM err", e.getMessage());
    }


    PendingIntent pendingIntent = PendingIntent.getActivity(context, 0 /* Request code */, intent, PendingIntent.FLAG_UPDATE_CURRENT);

    Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context, CHANNEL_ID)
            .setSmallIcon(R.drawable.icon_logo)
            .setColor(Color.parseColor("#531E6C"))  // small icon background color
            .setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.drawable.icon_sfach))
            .setContentTitle(Title)
            .setContentText(Message)
            .setAutoCancel(true)
            .setSound(defaultSoundUri)
            .setPriority(Notification.PRIORITY_MAX)
            .setDefaults(Notification.DEFAULT_ALL)
            .setContentIntent(pendingIntent);


    notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());


}

In activity

  Intent intent = getIntent();
    Bundle bundle = intent.getExtras();
    String url;
    if(bundle != null){

        url = bundle.getString("url");
        MediaPlayer mediaplayer = new MediaPlayer();
        try {
            mediaplayer.setDataSource(url);
            mediaplayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
            mediaplayer.prepareAsync();
            mediaplayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                @Override
                public void onPrepared(MediaPlayer mp) {
                    mediaplayer.start();
                }
            });
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

Upvotes: 0

Dromendokter
Dromendokter

Reputation: 35

The other way to do it, is use a module that has already implemented this for you. Technically you do the exact same thing that is described here, however with a single API call to providers like mBlox (http://developer.mblox.com), you'll be able to post your content and the devices you want to target, the hosting of the content, and the translation to a URL are being done for you, as well as sending the actual push notification.

Again, technically, it's the same as previous answers, however, for your personal integration it might be a quicker way to get to market.

Upvotes: 1

Related Questions