推送通知/ GCM不工作可能是什么问题?

时间:2016-10-17 17:25:54

标签: android firebase push-notification google-cloud-messaging firebase-cloud-messaging

我的GCM服务无效。我在清单文件中声明了一些类似于:

的内容
 <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
 <uses-permission android:name="android.permission.INTERNET" />
 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
 <uses-permission android:name="android.permission.READ_PHONE_STATE" />
 <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>

<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="com.google.android.c2dm.permission.SEND"/>
<uses-permission android:name="android.permission.GET_ACCOUNTS" />

<permission android:name="com.xxxx.permission.C2D_MESSAGE" android:protectionLevel="signature" />
<uses-permission android:name="com.xxxx.xxxxxx.permission.C2D_MESSAGE" />

<receiver android:name=".core.push.receiver.GCMBroadcastReceiver" android:exported="true" android:permission="com.google.android.c2dm.permission.SEND">
            <intent-filter>
                <action android:name="com.google.android.c2dm.intent.RECEIVE" />
                <action android:name="com.google.android.c2dm.intent.SEND" />
                <category android:name="com.xxxx.xxxxxx" />
            </intent-filter>
</receiver>
<service android:name=".core.push.service.GCMIntentService" />

我的广播接收器代码如下:

public class GCMBroadcastReceiver extends BroadcastReceiver
{
    @Override
    public void onReceive(Context context, Intent intent)
    {
        ComponentName messageProcessingService = new ComponentName(context.getPackageName(), GCMIntentService.class.getName());
        context.startService(intent.setComponent(messageProcessingService));
        setResultCode(Activity.RESULT_OK);
    }
}

我的意图服务:

public class GCMIntentService extends IntentService
{
    private static final int NOTIFICATION_NEW_MESSAGE = 0;

    public GCMIntentService()
    {
        super(GCMIntentService.class.getSimpleName());
    }

    @Override
    protected void onHandleIntent(Intent intent)
    {
        Bundle extras = intent.getExtras();
        GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);

        String messageType = gcm.getMessageType(intent);

        if (!extras.isEmpty())
        {
            if (!GoogleCloudMessaging.MESSAGE_TYPE_MESSAGE.equals(messageType))
            {
                return;
            }

            // TODO Make more efficient. Load message stream here, not in two places later!
            int newUnreadCount = MessagingController.getInstance().getUnreadCount() + 1;
            MessagingController.getInstance().prepareStream();
            MessagingController.getInstance().setUnreadCount(newUnreadCount);

            final boolean isUserAuthenticated = !TextUtils.isEmpty(AuthenticationController.getInstance().getAuthToken());

            if (isUserAuthenticated)
            {
                new Handler(Looper.getMainLooper()).post(new Runnable()
                {
                    @Override
                    public void run()
                    {
                        App.from(GCMIntentService.this).fire(MessagingEvent.NEW_MESSAGE);
                    }
                });
            }
            else
            {
                displayNotification(newUnreadCount + " New Message" + (newUnreadCount > 1 ? "s" : ""), newUnreadCount);
            }
        }
    }

    private void displayNotification(CharSequence message, int eventCount)
    {
        SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
        boolean shouldDisplayMessages = preferences.getBoolean("notifications_new_message", true);

        if (!shouldDisplayMessages)
        {
            return;
        }

        NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

        Intent openMessagingScreen = new Intent(this, LandingActivity.class);
        openMessagingScreen.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
        openMessagingScreen.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

        preferences.edit().putBoolean(MessagingFragment.PREF_MESSAGE_WAITING, true).apply();

        PendingIntent notificationAction = PendingIntent.getActivity(this, 0, openMessagingScreen, PendingIntent.FLAG_CANCEL_CURRENT);

        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
                .setSmallIcon(R.drawable.ic_notification)
                .setContentTitle(getString(R.string.app_name))
                .setStyle(new NotificationCompat.BigTextStyle().bigText(message))
                .setContentText(message)
                .setAutoCancel(true)
                .setContentIntent(notificationAction)
                .setNumber(eventCount);

        Notification notification = notificationBuilder.build();
        notification.defaults |= Notification.DEFAULT_ALL;

        try
        {
            notificationManager.notify(NOTIFICATION_NEW_MESSAGE, notification);
        }
        catch (SecurityException ex)
        {
            // This is required due to a bug in android
            // http://stackoverflow.com/questions/13602190/java-lang-securityexception-requires-vibrate-permission-on-jelly-bean-4-2
            Log.e("PPDirect", ex.getLocalizedMessage());
        }
    }
}

我有几个关于推送通知的问题:

  1. 哪个回叫函数实际检查到达的消息并在推送通知到达时被调用?
  2. 由于Google已将GCM更新为FCM,我是否可能需要更新密钥或将GCM迁移到FCM?
  3. 还有其他原因吗?
  4. 非常感谢任何有关此事的帮助。

1 个答案:

答案 0 :(得分:2)

  

1。哪个回调函数实际检查到达的消息,并在推送通知到达后调用?

对于Android,它通常在GcmListenerServicenlfilter中收到。但是,它也可能取决于应用程序是在后台还是前台。

  

2。是否有可能因为Google已将GCM更新为FCM,我需要更新密钥或将GCM迁移到FCM?

根据说明here

  

从2016年9月开始,只能使用“设置”面板的“云消息传递”选项卡在Firebase控制台中创建新的服务器密钥。可以在Firebase控制台中导入需要创建新服务器密钥的现有项目,而不会影响其现有配置。

GCM的新用户需要创建Firebase项目,无论您是否要使用FCM,都要拥有服务器密钥。对于迁移步骤,您可以看到我的回答here

  

3。可以有其他原因吗?

这太宽泛了,无法回答。也许是因为您的有效负载结构或onMessageReceived等的实现