无需单击托盘中的通知即可自动打开活动

时间:2017-02-14 20:21:56

标签: android firebase android-notifications firebase-cloud-messaging

我正在尝试制作一个应用程序,它会通过发送推送通知从Firebase触发手机。当通知到达时,我想打开紧急类,但它只在应用程序处于前台时调用紧急类。当应用程序在后台时,通知到达通知托盘,紧急类不会被执行。

此外,我正在尝试从数据库中提供消息,但由于某种原因,String为空。有人可以帮我找到解决方案或发布一个简单的例子吗?

谢谢。

class MyFirebaseMessagingService extends FirebaseMessagingService {
private Handler HandleIt = new Handler();
String messageContent = "";

public void onMessageReceived(RemoteMessage remoteMessage) {

    messageContent = remoteMessage.getData().get("message");
    showNotification(remoteMessage.getData().get("message"));
    postToastMessage(remoteMessage.getData().get("message"));
}

public void postToastMessage(final String message) {
    Handler handler = new Handler(Looper.getMainLooper());

    handler.post(new Runnable() {

        @Override
        public void run() {
            Toast.makeText(getApplicationContext(), message +" received", Toast.LENGTH_LONG).show();

            Intent newIntent = new Intent(MyFirebaseMessagingService.this, Emergency.class);
            newIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(newIntent);

        }
    });
}

private void showNotification(String message) {

    Intent i = new Intent(this, MainActivity.class);
    i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

    PendingIntent pendingIntent = PendingIntent.getActivity(this,0,i,PendingIntent.FLAG_UPDATE_CURRENT);


    NotificationCompat.Builder builder = (NotificationCompat.Builder) new NotificationCompat.Builder(this)
            .setAutoCancel(true)
            .setContentTitle(" Test")
            .setContentText(message)
            .setSmallIcon(R.drawable.info)
            .setContentIntent(pendingIntent);

    NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

    manager.notify(0,builder.build());
}
}

0 个答案:

没有答案