Android - How to set a notification to a specific date in the future?

后端 未结 2 1529
花落未央
花落未央 2020-12-14 22:29

Edit: SOLVED! Ever wanted to set a notification from a specific date starting a certain point in time (when an activity is started or when a button is pressed?) Read more to

相关标签:
2条回答
  • 2020-12-14 22:44

    One Important thing to note: When registering your broadcast receiver with an Intent-Filter, you need to add the exported attribute and set it to false. Like this:

    <service
    
            android:name=".utility.AlarmReceiver" android:exported="false">
            <intent-filter>
                <action android:name="NOTIFICATION_SERVICE" />
            </intent-filter>
    
        </service>
    

    Else components of other apps will be able to invoke or interact with your service.

    Full explanation from Google:

    android:exported
    
       Specifies whether or not components of other applications
       can invoke the service or interact with it —
       "true" if they can, and "false" if not.
    
    When the value is "false", only components of the same application or    
    applications with the same user ID can start the service or bind to it.
    
    The default value depends on whether the service contains intent filters.      
    The absence of any filters means that it can be invoked only by specifying 
    its exact class name. This implies that the service is intended only for 
    application-internal use (since others would not know the class name). So 
    in this case, the default value is "false". On the other hand, the presence 
    of at least one filter implies that the service is intended for external 
    use, so the default value is "true".
    
    0 讨论(0)
  • 2020-12-14 22:49

    Don't forget to give the below manifest permissions

    <uses-permission android:name="android.permission.VIBRATE" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    

    Your broadcast receiver registrations would be like this

       <receiver android:name=".AlarmReceiver" >
             <intent-filter>
               <action android:name="NOTIFICATION_SERVICE" />
             </intent-filter>
         </receiver>
    
    0 讨论(0)
提交回复
热议问题