Create Notification with BroadcastReceiver

前端 未结 3 1383
小鲜肉
小鲜肉 2021-01-17 09:08

I tried to create a Notification with this Code:

private void setNotificationAlarm(Context context) 
{
    Intent intent = new Intent(getApplicationContext()         


        
相关标签:
3条回答
  • 2021-01-17 09:31

    I think you need to use

    PendingIntent.getBroadcast (Context context, int requestCode, Intent intent, int flags)
    

    instead of getService

    0 讨论(0)
  • 2021-01-17 09:37

    you can use

    Intent switchIntent = new Intent(BROADCAST_ACTION);

    instead of using

    Intent intent = new Intent(getApplicationContext() , MyNotification.class);

    in here BROADCAST_ACTION is action that you are defining in manifest

    <receiver android:name=".MyNotification " android:enabled="true" >
        <intent-filter>
            <action android:name="your package.ANY_NAME" />
        </intent-filter>
    </receiver>
    

    you can catch it by using that action name

    public class MyNotification extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        String act = "your package.ANY_NAME";
            if(intent.getAction().equals(act)){
    
                //your code here
            }
    }}
    
    0 讨论(0)
  • 2021-01-17 09:42

    You use Notification.Builder to build the notification now and the pending intent needs to be PendingIntent.getBroadcast()

    0 讨论(0)
提交回复
热议问题