The phone I am trying to receive the push notification from is not receiving it when the user is offline and comes online, the regId and the result is returned so not sure what is stopping it, not receiving an error either...
EDIT: I have a user offline and then he would come online, and the message would not be pushed to him, only if the user is online at the time of the push...
Here is the code on server side:
// //BELOW FOR GCM
function notifyDetails(to, from, msg, itemId, itemName, fromName, type) {
User.findOne({_id: to}, function(err, results) {
if(err) {
throw err;
} else {
callNotify();
function callNotify() {
console.log("the from is " + results.reg_id);
if(results != null) {
request(
{ method: 'POST',
uri: 'https://android.googleapis.com/gcm/send',
headers: {
'Content-Type': 'application/json',
'Authorization': GOOGLE API KEY
},
"registration_ids": [results.reg_id],
"data": {
"notifyFromUserId": from,
"notifyMsg": msg,
"notifyItemId": itemId,
"notifyItemName": itemName,
"notifyFromName": fromName,
"notifyType": type
},
//default 4 weeks (this is in seconds)
//"time_to_live": 20000
})
}, function (error, response, body) {
if(error) {
throw error;
} else {
}
});
}
}
}
});
}
On android manifest file:
<receiver
android:name=".modular.MSGReceiver"
android:permission="com.google.android.c2dm.permission.SEND" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<category android:name="package.com" />
</intent-filter>
</receiver>
<service android:name=".modular.MSGService" />
The MSGService file:
public class MSGService extends IntentService {
SharedPreferences prefs;
NotificationCompat.Builder notification;
NotificationManager manager;
public MSGService() {
super("MSGService");
}
String TAG = Constants.DEBUG;
@Override
protected void onHandleIntent(Intent intent) {
Bundle extras = intent.getExtras();
GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);
String messageType = gcm.getMessageType(intent);
prefs = getSharedPreferences("Chat", 0);
if (!extras.isEmpty()) {
if (GoogleCloudMessaging.
MESSAGE_TYPE_SEND_ERROR.equals(messageType)) {
Log.d(TAG, "Error");
} else if (GoogleCloudMessaging.
MESSAGE_TYPE_DELETED.equals(messageType)) {
Log.d(TAG, "Error");
} else if (GoogleCloudMessaging.
MESSAGE_TYPE_MESSAGE.equals(messageType)) {
Log.d(TAG, "Received: " + extras.getString("notifyMsg"));
String notifyFromUserId, notifyMsg, notifyItemId, notifyItemName, notifyFromName;
int notifyType;
notifyFromUserId = extras.getString("notifyFromUserId");
notifyMsg = extras.getString("notifyMsg");
notifyItemId = extras.getString("notifyItemId");
notifyItemName = extras.getString("notifyItemName");
notifyFromName = extras.getString("notifyFromName");
notifyType = extras.getInt("notifyType");
sendNotification(notifyFromUserId, notifyMsg, notifyItemId, notifyItemName, notifyFromName, notifyType);
}
}
MSGReceiver.completeWakefulIntent(intent);
}
private void sendNotification(String notifyFromUserId, String notifyMsg, String notifyItemId,
String notifyItemName, String notifyFromName, int notifyType) {
//the data that you want passed to the new class
Bundle data = new Bundle();
Intent newIntentMsg = new Intent();
data.putString("userId", notifyItemId);
Intent profileIntent = new Intent(this, ProfileActivity.class);
profileIntent.putExtras(data);
newIntentMsg = profileIntent;
notification = new NotificationCompat.Builder(this);
notification.setContentTitle(notifyItemName);
notification.setContentText(notifyMsg);
notification.setTicker("New Message !");
notification.setSmallIcon(R.drawable.ic_launcher);
PendingIntent contentIntent = PendingIntent.getActivity(this, 1000,
newIntentMsg, PendingIntent.FLAG_CANCEL_CURRENT);
notification.setContentIntent(contentIntent);
notification.setAutoCancel(true);
manager =(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
manager.notify(0, notification.build());
}
}
The receiver file:
public class MSGReceiver extends WakefulBroadcastReceiver {
//Call a new intent and grab the data passed from the nodejs (extras)
@Override
public void onReceive(Context context, Intent intent) {
Bundle extras = intent.getExtras();
Intent msgrcv = new Intent(context, MSGService.class);
msgrcv.putExtra("notifyFromUserId", extras.getString("notifyFromUserId"));
msgrcv.putExtra("notifyMsg", extras.getString("notifyMsg"));
msgrcv.putExtra("notifyItemId", extras.getString("notifyItemId"));
msgrcv.putExtra("notifyItemName", extras.getString("notifyItemName"));
msgrcv.putExtra("notifyFromName", extras.getString("notifyFromName"));
msgrcv.putExtra("notifyType", extras.getInt("notifyType"));
LocalBroadcastManager.getInstance(context).sendBroadcast(msgrcv);
startWakefulService(context,msgrcv);
setResultCode(Activity.RESULT_OK);
}
}
In MSGReceveier.onReceve you do unneceessary work when repack intent. Just set component for existing intent and pass it to your service:
onReceive(Context context, Intent intent) { intent.setComponent(new ComponentName(context.getPackageName(), MSGService.class.getName()); startWakefulService(context,msgrcv); setResultCode(Activity.RESULT_OK); }
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.