Start service through user action

1

Good morning! Home I have an application that will receive PushNotification. Home But the user can select whether or not to receive the notifications.

I have WakefulBroadcastReceiver and IntentService that check for a message.
These, start automatically, with the application. Home I wonder if there is a possibility to start this service only when the user selects the option. and not when to start the application? Home Declaration in AndroidManifest.xml

 <receiver
            android:name=".services.GcmBroadcastReceiver"
            android:permission="com.google.android.c2dm.permission.SEND" >
            <intent-filter>
                <action android:name="com.google.android.c2dm.intent.RECEIVE" />
                <category android:name="br.com.app.auto.application.AutoApplication" />
            </intent-filter>
        </receiver>
        <service android:name=".services.NotificationService" />

Thank you for your cooperation!
Greetings!

    
asked by anonymous 22.07.2015 / 16:56

2 answers

1

You can try registering a receiver via code in your application, but I did not find any examples of this and probably will not work. GCM samples all register the receiver in Android Manifest.xml.

This OS answer in English says you need to be so the push notification should be able to "wake up" (start) the application if it is not currently running. If receiver was registered via code, push notification would be lost if the application was not running when it arrived. The answer refers to C2DM, which is the previous version of GCM, but probably in GCM it remains so for the same reason.

It is also not recommended to register and "unregister" the Registration ID to control the sending of the pushes , according to the documentation. This ID must be received once, be stored in the application, and be in use until the application is uninstalled by the user.

The way to do this is then to keep an extra field in your device table registered on the server (or your user table, or user settings, you decide) indicating whether or not the user wants to receive the push notifications , and make web services available to change this configuration via the application. This approach can be used to control user preference and also to allow it to only receive pushes while logged into the application.

    
22.07.2015 / 19:35
0

Remove this action from the

    <receiver android:name=".NOMEDORECEIVER">
                <intent-filter>
este ---->       <action android:name="android.intent.action.BOOT_COMPLETED" />
                </intent-filter>
            </receiver>

So it will not boot into the android

    
22.07.2015 / 17:33