广播接收器停止触发" onReceive(上下文上下文,意图意图)"从最近的清单中清除(滑动)应用程序后的方法,如何解决这个问题?
清单代码
<receiver
android:name="*.receiver.TestReceiver"
android:enabled="true"
android:exported="true"
android:permission="com.google.android.c2dm.permission.SEND" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />
<action android:name="com.google.android.c2dm.intent.REGISTER" />
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.USER_PRESENT" />
<category android:name="pacakge" />
</intent-filter>
</receiver>
接收者代码
public void onReceive(Context context, Intent intent) {
LogUtils.LOGD("notfy", "Receiver : ");
if(intent!=null){
Bundle extras = intent.getExtras();
if(extras!=null) {
try {
String regId = extras.getString("registration_id");
String message = extras.getString("message");
String qsid = extras.getString("id","");
String type = extras.getString("type","");
// extras.getString("aps");
/*code*/
} catch (Exception e){
LogUtils.LOGD("notfy", "onReceive last exception: " + e.getMessage());
}
}
}
}
答案 0 :(得分:1)
在主应用中启动/停止服务
Intent service = new Intent(context, MyService.class);
context.startService(service);
...
Intent service = new Intent(context, MyService.class);
context.stopService(service);
服务
public class MyService extends Service
{
private static BroadcastReceiver m_Receiver;
@Override
public IBinder onBind(Intent arg0)
{
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return START_STICKY;
}
@Override
public void onCreate()
{
Receiver();
}
@Override
public void onDestroy()
{
unregisterReceiver(m_Receiver);
}
private void Receiver()
{
m_Receiver = new BroadcastReceiver()
{
@Override
public void onReceive(Context context, Intent intent)
{
//place all ur stuff here
}
}
}
}
答案 1 :(得分:0)
看起来问题出在服务器端,
https://firebase.google.com/docs/cloud-messaging/concept-options
有两种类型的消息
使用FCM,您可以向客户端发送两种类型的消息:
通知消息,有时被视为&#34;显示消息。&#34; 数据消息,由客户端应用程序处理。
基本上从服务器上的通知更改为数据,这将在应用关闭时调用OnRecieve。
查看链接以获取更多信息和示例