简体   繁体   English

GCM在登录后未将推送通知发送给离线用户……不知道我缺少了什么吗?

[英]GCM is not sending the push notification to offline user upon login… dont know what I am missing?

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... 当用户离线并上线时,我尝试从其接收推送通知的电话未收到它,regId和结果返回,因此不确定是什么在阻止它,也未收到错误...

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: 在Android清单文件上:

<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: MSGService文件:

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);
    }
}
  1. What response you receive from GCM server when try to send push message? 尝试发送推送消息时,您从GCM服务器收到什么响应? Please post response here. 请在此处发布回复。 Also you can diagnose where is your problem with my test push server help. 您也可以通过我的测试推送服务器帮助来诊断问题所在。
  2. In MSGReceveier.onReceve you do unneceessary work when repack intent. 在MSGReceveier.onReceve中,重新包装意图时您不需要做任何工作。 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); } 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM