2017-08-25 121 views
-1

我正在創建一個出租車應用程序。每當乘客請求乘坐時,請求將由服務器處理,並通知出租車司機。驅動程序應用程序包含一個具有警報的活動,在接收到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()); 

回答

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); 
} 

我希望它有幫助。

相關問題