我尝试从新用户添加到数据库(我正在使用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);
}
答案 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);