Start service through user action

Asked

Viewed 103 times

1

Good morning!
I have an app that will receive Pushnotification.
But the user will be able to select whether or not to receive notifications.

I have a WakefulBroadcastReceiver and a IntentService that check for a message.
These, start automatically, with the application.
I would like to know there is possibility to start this service only when the user selects the option. and not when to launch the application?
Statement 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" />

Thanks for your cooperation!
Greetings!

2 answers

1


You can try registering a receiver via code in your app, but I haven’t found any examples of this and it probably won’t work. GCM examples all record the receiver in Android Manifest.xml.

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

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

The way then is to keep an extra field in your table of devices registered on the server (or in your user table, or user settings, anyway, you decide) indicating whether the user wants to receive or not push Notifications, and make available web services to change this setting via app. This approach can be used to control the user’s preference and also to allow him to only receive pushes being logged in to the application.

  • 1

    OK! I thought about stopping the service, because if the user does not want to receive the notifications, there is no need to run the Service. The server control will also be done, to avoid unnecessary processing! Thank you very much for the help!

0

Remove this action from the tag

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

So it will not start on android boot

  • 1

    I have removed, but when I start the Service ( getActivity().startService(new Intent(getActivity(), Gcmbroadcastreceiver.class)); ) it does not work!

  • I’m sorry, I think I got the explanation wrong, I’ll edit.

  • So... you don’t even have that line!

  • Put your code in the question please, maybe it’s easier to find the problem

  • Actually I don’t have the code! is exactly this the question... how to do? Thank you!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.