【问题标题】:The same push notification keeps appearing whenever i open my apps每当我打开我的应用程序时,都会出现相同的推送通知
【发布时间】:2016-05-17 15:50:13
【问题描述】:

尽管我已经清除了通知栏中的通知,但每当我重新打开我的应用程序时,都会出现相同的推送通知。其次,我如何实现一个服务,以便我的应用可以在应用关闭时收到通知。

 @Override
public IBinder onBind(Intent intent) {
return null;
}


@Override

public int onStartCommand(Intent intent, int flags, int startId) {

SharedPreferences sharedPreferences = getSharedPreferences(Constants.SHARED_PREF, MODE_PRIVATE);


String id = sharedPreferences.getString(Constants.UNIQUE_ID, null);


Firebase firebase = new Firebase(Constants.FIREBASE_APP + id);



firebase.addValueEventListener(new ValueEventListener() {


    @Override
    public void onDataChange(DataSnapshot snapshot) {

        String msg = snapshot.child("msg").getValue().toString();


        if (msg.equals("none"))
            return;

        showNotification(msg);
    }

    @Override
    public void onCancelled(FirebaseError firebaseError) {
        Log.e("The read failed: ", firebaseError.getMessage());
    }
});

return START_STICKY;
 }


private void showNotification(String msg){
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setSmallIcon(R.mipmap.ic_launcher);
Intent intent = new Intent(NotificationListener.this,ViewRecord.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
builder.setContentIntent(pendingIntent);
Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
builder.setSound(alarmSound);
builder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher));
builder.setContentTitle("Notifier");
builder.setContentText(msg);
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
builder.setAutoCancel(true);
notificationManager.notify(1, builder.build());
}

我的服务代码如下。我在第一个活动的 onCreate 函数中调用该服务..

public class MyService extends Service {
public MyService() {
}

@Override
public IBinder onBind(Intent intent) {
    // TODO: Return the communication channel to the service.
    throw new UnsupportedOperationException("Not yet implemented");
}

@Override
public void onCreate() {
    Toast.makeText(this, "The new Service was Created", Toast.LENGTH_LONG).show();

}

@Override
public void onStart(Intent intent, int startId) {
    // For time consuming an long tasks you can launch a new thread here...
    Toast.makeText(this, " Service Started", Toast.LENGTH_LONG).show();

}
 }

【问题讨论】:

  • 你是启动服务还是只绑定服务?
  • @Masum 查看我刚刚添加的服务编码....我的服务编码是否正确?
  • 服务将在您打开应用程序时启动,因为显然您是在 MainActivity 的 OnCreate 中启动服务。现在,只要在您正在运行的服务中调用 onDataChange,就会触发通知。你需要使用一个布尔标志来检查服务是否已经在运行,并在你每次打开你的应用程序时相应地处理服务的启动。
  • @AmitTumkur 你能指导我更多关于编码布尔标志来检查服务吗?我是新手,所以完全不知道该怎么做哈哈...谢谢

标签: android android-studio push-notification


【解决方案1】:

将此作为答案发布,因为注释 wud 中的代码使其看起来不结构化 isServiceStarted

public class MainActivity extends AppCompatActivity {
private SharedPreferences servicePref;
private boolean isServiceStarted;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    servicePref = getSharedPreferences("servicePref", MODE_PRIVATE);
    isServiceStarted = servicePref.getBoolean("isServiceStarted", false);

    if (!isServiceStarted) {
        startService(new Intent(this, MyService.class));
        servicePref.edit().putBoolean("isServiceStarted",true).apply();
    }
}

并且在你的 MyService.class 里面的 onStop 方法中做到这一点不会失败。

public class MyService extends Service {
     @Nullable
     @Override
     public IBinder onBind(Intent intent) {
         return null;
     }

     @Override
     public void onDestroy() {
         super.onDestroy();
         // save value as false when service gets destroyed so as to start again when u open the app
         getSharedPreferences("servicePref", MODE_PRIVATE).edit().putBoolean("isServiceStarted",false).apply();
     }
}

【讨论】:

  • 感谢您的帮助....但是如果我的服务停止了,我的应用程序在关闭时还会收到通知吗?
  • 接收通知由广播接收器处理,因此即使应用程序没有运行,广播接收器也会接收所需的动作广播并执行其 onReceive 方法中提到的代码。
  • 我再次需要您在广播接收器上的帮助,可以再帮我一次吗?抱歉给你带来麻烦,但我真的需要你的帮助....非常感谢
  • 我之前的评论是一般性的回答。在你的情况下,只要 onDataChange 被调用,你就会从 MyService 创建或显示推送通知。因此,在服务运行之前,您将能够显示通知。并返回 START_STICKY 告诉 android os 如果服务被杀死则重新创建服务。所以这很好,
  • 还是一样,每当我关闭和打开应用程序时都会发出相同的通知:(
【解决方案2】:

覆盖 onStartCommand() 方法,然后返回 START_STICKY。

【讨论】:

  • 我认为 onStartCommand() 已经覆盖,如上面的编码所示?
  • 它包含了返回 START_STICKY
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-02
  • 1970-01-01
  • 1970-01-01
  • 2017-07-31
  • 1970-01-01
相关资源
最近更新 更多