无法使用Firebase从通知中启动特定活动

时间:2017-06-02 12:23:12

标签: android android-activity android-service android-notifications

我尝试从新用户添加到数据库(我正在使用firebase数据库)时更新的通知中启动活动。

通知完美无缺,但每当我尝试从通知中启动UserDashboardActivity时。它不起作用。

任何人都可以帮助我。

我的代码:

此处服务

中的通知代码
public class NotificationService extends Service {
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        notifyMe(order.amount);
        return START_STICKY;
    }

    private void notifyMe(String amount){
    NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
            .setSmallIcon(R.drawable.add_order)
            .setContentTitle("New Order")
            .setContentText("Order amount: " + amount)
            .setAutoCancel(true);

    Intent notificationIntent = new Intent(this,UserDashboardActivity.class);
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    PendingIntent contentIntent = PendingIntent.getActivity(this,0,notificationIntent,PendingIntent.FLAG_UPDATE_CURRENT);
    builder.setContentIntent(contentIntent);

    NotificationManager manager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
    manager.notify(0,builder.build());
    }
}

我在UserDoashboardActivity停止时启动服务

@Override
protected void onStop() {
    super.onStop();
    Intent intent = new Intent(UserDashboardActivity.this, NotificationService.class);
    startService(intent);
}

1 个答案:

答案 0 :(得分:0)

在notifyMe()函数中尝试此代码...

  Intent intent = new Intent(this, YourActivityName.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
            PendingIntent.FLAG_ONE_SHOT);