简体   繁体   中英

Calling a notification in onReceive of broadcast receiver

    public class MyBroadcaseciver extends BroadcastReceiver {

    MediaPlayer mymedia;
    @Override
    public void onReceive(Context context, Intent intent) {
        mymedia=MediaPlayer.create(context,R.raw.alarm);
        mymedia.start();
        Toast.makeText(context, "Alarm....", Toast.LENGTH_LONG).show();



    }



}

The code above is my broadcast receiver, which plays a song when it fires. This is working as expected, however I wish to call a push notification to pop here and also my notification isn't working here.

Notification.Builder bulider = new Notification.Builder(this)
                .setContentTitle("Rainfall Alert")
                .setContentText("Todays Rain");
        Notification notification = bulider.build();
        NotificationManager manager = (NotificationManager)
                getSystemService(Context.NOTIFICATION_SERVICE);
        manager.notify(0,notification);

Help is appreciated.

public void sendNotification(String title, String body, Intent intent,int pushid) {

    Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context)
            .setSmallIcon(R.mipmap.ic_launcher)
            .setContentTitle(title)
            .setStyle(new NotificationCompat.BigTextStyle()
                    .bigText(body))
            .setContentText(body)
            .setAutoCancel(true)
            .setSound(defaultSoundUri);

    if(intent!=null) {
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent,
                PendingIntent.FLAG_ONE_SHOT);
        notificationBuilder.setContentIntent(pendingIntent);
    }


    NotificationManager notificationManager =
            (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);

    notificationManager.notify(0, notificationBuilder.build());
}

call this funcation in onrecive method

Intent intent = new Intent(context, Mainscreen.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
sendNotification("title","message", intent, 0);
public class MyBroadcaseciver extends BroadcastReceiver {

MediaPlayer mymedia;

   @Override
   public void onReceive(Context context, Intent intent) {

      mymedia=MediaPlayer.create(context,R.raw.alarm);
      mymedia.start();
      Toast.makeText(context, "Alarm....", Toast.LENGTH_LONG).show();

       Intent intent = new Intent(context, Mainscreen.class);
       intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
       sendNotification("title","message", intent, 0);

   }

   public void sendNotification(String title, String body, Intent intent,int pushid) {

     Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
     NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context)
        .setSmallIcon(R.mipmap.ic_launcher)
        .setContentTitle(title)
        .setStyle(new NotificationCompat.BigTextStyle()
                .bigText(body))
        .setContentText(body)
        .setAutoCancel(true)
        .setSound(defaultSoundUri);

   if(intent!=null) {
      intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
      PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent,
            PendingIntent.FLAG_ONE_SHOT);
      notificationBuilder.setContentIntent(pendingIntent);
            }


    NotificationManager notificationManager =
        (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);

    notificationManager.notify(0, notificationBuilder.build());

    }
 } 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM