我需要帮助。我的应用程序在应用程序处于打开状态或后台时会收到消息,但如果我关闭设备或强制关闭应用程序,则重新启动应用程序时永远无法收到所有消息。我读了很多东西,但我找不到任何东西。请有我的课程。
1有人给我发消息。我收到了这条消息。
2我强制关闭我的应用程序或关闭我的设备。
3有人给我发了一条消息,但我的设备已关闭。
4我打开了设备,然后重启了应用。我没有收到任何消息,但如果有人现在给我发新消息,我会收到此消息。
仅当应用处于背景或正面时。但是当我在应用程序被杀后重新启动应用程序时,我没有收到来自gcm的存储消息。
这是ReceiverClass:
public class MSGReceiver extends WakefulBroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Bundle extras = intent.getExtras();
Intent msgrcv = new Intent("Msg");
msgrcv.putExtra("msg", extras.getString("msg"));
msgrcv.putExtra("fromu", extras.getString("fromu"));
msgrcv.putExtra("fromname", extras.getString("name"));
if(extras.getString("fromu")!=null && extras.getString("msg")!=null){
try{
SharedPreferences blindDate = context.getSharedPreferences("datosblind",Context.MODE_PRIVATE);
ConexionSQLite conexion = new ConexionSQLite(context);
int id = Integer.parseInt(extras.getString("fromu"));
int idUser = Integer.parseInt(blindDate.getString("REG_FROM",""));
int order = conexion.getSize(id, idUser);
conexion.insertNewMessage(id, extras.getString("msg"), order+1, "false", idUser);
conexion.insertNewSize(id, order+1, idUser);
}catch(Exception e){}
}
LocalBroadcastManager.getInstance(context).sendBroadcast(msgrcv);
ComponentName comp = new ComponentName(context.getPackageName(),MSGService.class.getName());
startWakefulService(context, (intent.setComponent(comp)));
setResultCode(Activity.RESULT_OK);
}
}
这是ServiceClass:
public class MSGService extends IntentService {
SharedPreferences prefs;
NotificationCompat.Builder notification;
NotificationManager manager;
public MSGService() {
super("MSGService");
}
@Override
protected void onHandleIntent(Intent intent) {
Bundle extras = intent.getExtras();
GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);
String messageType = gcm.getMessageType(intent);
prefs = getSharedPreferences("datosblind", 0);
if (!extras.isEmpty()) {
if (GoogleCloudMessaging.
MESSAGE_TYPE_SEND_ERROR.equals(messageType)) {
Log.e("L2C","Error");
} else if (GoogleCloudMessaging.
MESSAGE_TYPE_DELETED.equals(messageType)) {
Log.e("L2C","Error");
} else if (GoogleCloudMessaging.
MESSAGE_TYPE_MESSAGE.equals(messageType)) {
if(!prefs.getString("CURRENT_ACTIVE","").equals(extras.getString("fromu"))) {
if(prefs.getInt("iniciadaLaSesion", 0)==1)
sendNotification(extras.getString("msg"), extras.getString("fromu"), extras.getString("name"));
}
}
}
MSGReceiver.completeWakefulIntent(intent);
}
private void sendNotification(String msg,String mobno,String name) {
Bundle args = new Bundle();
args.putString("mobno", mobno);
args.putString("name", name);
args.putString("msg", msg);
Intent chat = new Intent(this, Conversacion.class);
chat.putExtra("INFO", args);
notification = new NotificationCompat.Builder(this);
notification.setContentTitle(name);
notification.setContentText(msg);
notification.setTicker("Blind Date: " + name);
notification.setSmallIcon(R.drawable.ic_launcher);
notification.setSound(Uri.parse("android.resource://"
+ this.getPackageName() + "/" + R.raw.sonidonotificacion));
PendingIntent contentIntent = PendingIntent.getActivity(this, 1000,
chat, PendingIntent.FLAG_CANCEL_CURRENT);
notification.setContentIntent(contentIntent);
notification.setAutoCancel(true);
manager =(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
manager.notify(Integer.parseInt(mobno), notification.build());
}
}
这是清单:
<receiver
android:name="com.expansion.minlove.MSGReceiver"
android:permission="com.google.android.c2dm.permission.SEND" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<category android:name="com.expansion.minlove" />
</intent-filter>
</receiver>
<service android:name="com.expansion.minlove.MSGService" />
这是Server的请求:
exports.send = function(fromn,fromu,to,msg,callback) {
user.find({mobno: to},function(err,users){
var len = users.length;
if(len == 0){
callback({'response':"Failure"});
}else{
var to_id = users[0].reg_id;
var name = users[0].name;
request(
{ method: 'POST',
uri: 'https://android.googleapis.com/gcm/send',
headers: {
'Content-Type': 'application/json',
'Authorization':'key=********mjxGkTrOnH6dE'
},
body: JSON.stringify({
"registration_ids" : [to_id],
"data" : {
"msg":msg,
"fromu":fromu,
"name":fromn
},
})
}
, function (error, response, body) {
callback({'response':"Success"});
}
)
}});
}
&#13;
我需要帮助。 我会发疯的。
答案 0 :(得分:0)
更改为:
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<category android:name="com.example.gcm" />
</intent-filter>