gopinath-yd opened a new issue, #1733:
URL: https://github.com/apache/cordova-android/issues/1733

   Hi i'm trying to keep active my app to read my fcm notification using tts 
and to play media once fcm notification received if app is killed or closed or 
terminated, i tried so many methods it doesn't work for me so please help me to 
fix this issue and i attached my code 
   
   ### Myforegroundservice
   package com.fcm.test;
   
   import android.app.Notification;
   import android.app.Service;
   import android.content.Intent;
   import android.media.MediaPlayer;
   import android.net.Uri;
   import android.os.IBinder;
   import androidx.core.app.NotificationCompat;
   
   public class MyForegroundService extends Service {
       private MediaPlayer mediaPlayer;
       private static final String CHANNEL_ID = "ForegroundServiceChannel";
   
       @Override
       public int onStartCommand(Intent intent, int flags, int startId) {
           // Set up the notification for the foreground service
           Notification notification = new NotificationCompat.Builder(this, 
CHANNEL_ID)
               .setContentTitle("Playing Audio")
               .setContentText("Audio is playing in the background")
               .setSmallIcon(R.drawable.ic_music_note)  // Replace with your 
own icon
               .build();
   
           startForeground(1, notification);
   
           // Play the audio file
           String filePath = "android.resource://" + getPackageName() + 
"/raw/sample";
           mediaPlayer = MediaPlayer.create(this, Uri.parse(filePath));
           mediaPlayer.start();
   
           return START_NOT_STICKY;
       }
   
       @Override
       public void onDestroy() {
           super.onDestroy();
           if (mediaPlayer != null) {
               mediaPlayer.stop();
               mediaPlayer.release();
           }
       }
   
       @Override
       public IBinder onBind(Intent intent) {
           return null;
       }
   }
   
   ### MyFirebaseMessagingService
   package com.fcm.test;
   
   import android.content.Context;
   import android.content.Intent;
   import androidx.core.content.ContextCompat;
   
   import com.google.firebase.messaging.FirebaseMessagingService;
   import com.google.firebase.messaging.RemoteMessage;
   
   public class MyFirebaseMessagingService extends FirebaseMessagingService {
       @Override
       public void onMessageReceived(RemoteMessage remoteMessage) {
           if (remoteMessage.getData().containsKey("status") && 
"1".equals(remoteMessage.getData().get("status"))) {
               // Start foreground service to play audio
               Intent serviceIntent = new Intent(this, 
MyForegroundService.class);
               ContextCompat.startForegroundService(this, serviceIntent);
           }
       }
   }
   
   ### Version information
   
   I'm using cordova android 13.0.0
   
   and is there anyother way to keep active like headless


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to