繁体   English   中英

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

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

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

编辑:我有一个用户脱机,然后他将联机,并且仅当用户在推送时处于联机状态时,消息才会被推送给他...

这是服务器端的代码:

// //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 {

                    }

                });
                }

        }
            }


    });



}

在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" />

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

接收者文件:

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. 尝试发送推送消息时,您从GCM服务器收到什么响应? 请在此处发布回复。 您也可以通过我的测试推送服务器帮助来诊断问题所在。
  2. 在MSGReceveier.onReceve中,重新包装意图时您不需要做任何工作。 只需为现有意图设置组件并将其传递给您的服务即可:

     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