0

我正在处理 android 推送通知。我在 Google api 控制台上创建了应用程序:

还提供所有权限,但我仍然得到空的注册 ID。

这是我的代码:

public void registerGCMNotifications(){
        GCMRegistrar.checkDevice(this);
        GCMRegistrar.checkManifest(this);
        String regId = GCMRegistrar.getRegistrationId(this);
        if (regId.equals("")) {
            GCMRegistrar.register(this, "sender_id");//sender id
            Constants.REGISTRATION_ID = GCMRegistrar.getRegistrationId(this);
        } else {
            System.out.println(regId);
        }
    }

此代码返回空的registrationId。

这是 GCMIntentService 的代码:

package com.interactive.pushnotification;

import java.text.SimpleDateFormat;
import java.util.Calendar;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

import com.example.interactiveapp.R;
import com.google.android.gcm.GCMBaseIntentService;
import com.interactiveapp.Constants;

public class GCMIntentService extends GCMBaseIntentService {

    public GCMIntentService() {
        super("sender_id");
    }

    @Override
    protected void onRegistered(Context arg0, String registrationId) {
        Constants.REGISTRATION_ID=registrationId;
        System.out.println("ID: "+registrationId);
    } 

    @Override
    protected void onUnregistered(Context arg0, String arg1) {
    }

    @Override
    protected void onMessage(Context arg0, Intent arg1) {
        String message = arg1.getStringExtra("message");
        //displayNotification(message);
    }

    @Override
    protected void onError(Context arg0, String errorId) {
        Log.i(TAG, "Received error: " + errorId);
    }

    @Override
    protected boolean onRecoverableError(Context context, String errorId) {
        return super.onRecoverableError(context, errorId);
    }


    @SuppressWarnings("deprecation")
    public void displayNotification(String message)
    {
        //int count=Constant.pushNotificationsCount++;
        // this
        NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

        // Get Device Date Time...
        Calendar cl = Calendar.getInstance();
        SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy HH:mm a");
        String Strtdates = sdf.format(cl.getTime());

        long when = System.currentTimeMillis();         
        Context context = getApplicationContext();     
        CharSequence contentTitle = "";  

        Intent notificationIntent = new Intent(this, GCMIntentService.class);
        PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
        Notification notification = new Notification(R.drawable.ic_launcher, "H",0);

        notification.defaults |= Notification.DEFAULT_SOUND;
        notification.defaults |= Notification.DEFAULT_VIBRATE;

        notification.setLatestEventInfo(context, contentTitle, message, contentIntent);

        // and this
        mNotificationManager.notify(0, notification);
        //System.out.println(count);

    }
}
4

1 回答 1

0

GCMRegistrar.getRegistrationId(this)第一次运行应用程序时将返回一个空的注册 ID。GCMRegistrar.register(this, "sender_id")是异步的。其结果将由 返回onRegistered(Context arg0, String registrationId),因此如果GCMRegistrar.getRegistrationId(this)返回一个空的 reg ID,则期望从 接收 reg ID onRegistered

于 2013-06-05T14:27:26.660 回答