android - 如何在 PubNub Android 中接收推送通知???

标签 android pubnub

在 pubnub android 中接收推送通知的广播接收器是什么。

最佳答案

PubNub 发布了一篇博文,其中全面介绍了使用他们的 Android SDK 发送和接收 GCM。

Sending and Receiving Android Push Notifications w/ GCM

总而言之,接收器和 Intent 过滤器应如下所示:

<receiver android:name=".GcmBroadcastReceiver" 
          android:permission="com.google.android.c2dm.permission.SEND" > 
    <intent-filter> 
        <action android:name="com.google.android.c2dm.intent.RECEIVE" /> 
        <category android:name="your.package.name" /> 
    </intent-filter> 
</receiver> 
<service android:name=".GcmIntentService" /> 
<meta-data android:name="com.google.android.gms.version" 
           android:value="@integer/google_play_services_version" /> 

你的 GcmBroadcastReceiver 应该看起来像这样:

public class GcmBroadcastReceiver extends WakefulBroadcastReceiver { 
    @Override 
    public void onReceive(Context context, Intent intent) { 
        ComponentName comp = new ComponentName(context.getPackageName(), GcmIntentService.class.getName()); 
        startWakefulService(context, (intent.setComponent(comp))); 
        setResultCode(Activity.RESULT_OK); 
    } 
}

最后,GcmIntentService:

public class GcmIntentService extends IntentService {   
    public GcmIntentService() { 
        super("GcmIntentService"); 
    }   

    @Override protected void onHandleIntent(Intent intent) { 
        Bundle extras = intent.getExtras(); 
        GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this); 
        String messageType = gcm.getMessageType(intent);   
        if (!extras.isEmpty() && GoogleCloudMessaging.MESSAGE_TYPE_MESSAGE) {
            sendNotification("Received: " + extras.toString()); 
        } 
        GcmBroadcastReceiver.completeWakefulIntent(intent); 
    }
    ...
}

如果您希望看到我没有涵盖的任何内容,请查看博客。如果您在那里没有看到它或仍然遇到问题,请告诉我,我很乐意为您详细说明与 PubNub GCM 相关的任何内容!

关于android - 如何在 PubNub Android 中接收推送通知???,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29553813/

相关文章:

ios - PubNub iOS 应用程序不接收推送通知

ios - 适用于 iOS 的 Pusher 与 PubNub

android - 没有关闭按钮的 Eclipse Android SDK 管理器窗口

angularjs - Laravel:使用 Angular 制作实时应用程序

Android - ListView - 按需调用 getView()

java - 如何在后台线程中执行 run() 之外的方法?

ios - 无法向 iOS 发送/接收 PubNub 推送通知

laravel-5 - laravel 对于实时应用程序有什么好处?

android - Xamarin Android 使背景透明

android - 如何为 Android 创建电子邮件,使其适合屏幕/缩小/缩放,就像在 iOS 中一样