Android推送通知未获得注册ID

时间:2013-06-05 10:50:46

标签: android push-notification

我正在研究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);

    }
}

1 个答案:

答案 0 :(得分:0)

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