簡體   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