androidforeground-service

Which use foregroundServiceType to MESSAGE_EVENT in Android target SDK34?


I migrate TargetSDK34 in Android. AndroidManifest tag occured error line. So I had search Android Document and that was confused to me what I use it?.

Message_EVENT is which I use dataSync or remoteMessaging or something else? Please suggestion solved way for me!

Thanks!

    <service android:name=".Push.PushIntentService"
        android:exported="false"
        android:foregroundServiceType="dataSync" >   <!--here!!-->
        <intent-filter>
            <action android:name="com.google.firebase.MESSAGING_EVENT" />
        </intent-filter>
    </service>


Solution

  • If your foreground service is used for:

    "Transfer text messages from one device to another. Assists with continuity of a user's messaging tasks when they switch devices." Reference

    then you need to mark it's type as remoteMessaging

    You do this by doing three things:

    1. In your AndroidManifest.xml file, you add the foregroundServiceType attribute with the value of remoteMessaging

    2. You need to add the following permission in your AndroidManifest.xml file FOREGROUND_SERVICE_REMOTE_MESSAGING. This permission's level is normal so there is no need to do anything else

    3. When you start the foreground service with startForeground, pass the following constant as the third argument there FOREGROUND_SERVICE_TYPE_REMOTE_MESSAGING

     <uses-permission android:name="android.permission.FOREGROUND_SERVICE_REMOTE_MESSAGING"/>
    
     <service
            android:name=".YourServiceName"
            android:foregroundServiceType="remoteMessaging"
            android:exported="false">