当应用处于后台或完全关闭时,启动点击通知的活动

时间:2017-08-25 15:11:17

标签: android

我正在创建一个出租车应用程序。每当乘客请求乘车时,该请求由服务器处理并且通知出租车司机。驱动程序应用程序包含一个带有警报的活动,该活动将在收到FCM通知时开始振铃。当应用程序处于前台时,FCM运行良好,但当应用程序处于后台或处于关闭状态时,它不会启动警报活动。有没有办法从通知中启动预期的待处理意图,即使应用程序在后台也是如此。

Intent intent = new Intent(this, AlarmActivity.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);

    Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
            .setSmallIcon(R.drawable.alert)
            .setContentTitle("Firebase Push Notification")
            .setContentText(messageBody)
            .setAutoCancel(true)
            .setSound(defaultSoundUri)
            .setContentIntent(pendingIntent);

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

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

1 个答案:

答案 0 :(得分:0)

为此目的使用BroadcastReciever

public class Alarm extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
    PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
    PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP, "");
    wl.acquire();


    Toast.makeText(context, "Alarm !!!!!!!!!!", Toast.LENGTH_LONG).show(); // For example

    intent = new Intent();
    intent.setClass(context, Test.class); //Test is a dummy class name where to redirect
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    intent.putExtra("msg","Task Pending");
    context.startActivity(intent);
    wl.release();
}

并在您的测试活动中按您的意愿响铃。

public class Test extends AppCompatActivity {

private Ringtone r;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
    setContentView(R.layout.activity_alarm);
    Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
    r = RingtoneManager.getRingtone(this, notification);
    r.play();

    ImageView buttonCancelAlarm = (ImageView) findViewById(R.id.buttonCancelAlarm);

    buttonCancelAlarm.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            cancelAlarm(Test.this);
            finish();
        }
    });

}
public void cancelAlarm(Context context)
{
    r.stop();
    Intent intent = new Intent(context, Alarm.class);
    PendingIntent sender = PendingIntent.getBroadcast(context, 12345, intent, 0);
    AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    alarmManager.cancel(sender);
}

我希望它有所帮助。