2016-05-17 104 views
0

每当我重新打开应用程序时,同样的推送通知不断出现,尽管我已经清除了栏中的通知。以及如何实施服务,以便我的应用程序可以收到通知,尽管应用程序已关闭。每当我打开我的应用程序时,推送通知不断出现

@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(); 

} 
} 
+0

你开始你的服务还是只绑定你的服务? – Masum

+0

@Masum查看我的服务编码,我刚刚添加....是我的服务编码正确吗? –

+0

@Masum hi there –

回答

1

重写onStartCommand()方法,然后返回START_STICKY。

+0

我认为onStartCommand()已覆盖如上面的代码所示? –

+0

,它包含了返回START_STICKY –

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

和方法的onStop里面乌尔MyService.class做到这一点没有失败。

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(); 
    } 
} 
+0

thx以帮助...但如果我的服务停止,应用程序关闭时我的应用程序是否仍会收到通知? –

+0

接收通知由广播接收器处理,所以即使应用程序未运行,广播接收器也会收到所需的动作广播并执行onReceive方法中提到的代码。 –

+0

再次我需要你的广播接收器的帮助,可以帮助我多一次?因为造成你的麻烦,但我真的需要你的帮助....非常感谢 –

相关问题