【问题标题】:Push Notification Android working alright previous, stops abruptly推送通知 Android 之前工作正常,突然停止
【发布时间】:2014-03-24 10:55:57
【问题描述】:

通过我的应用程序中的 gcm.jar 在应用程序中集成推送通知,该应用程序正常工作到 8 小时前。我知道那是我收到最后一封邮件的时间。

这是我的 GCM 意图服务:

public class GCMIntentService extends GCMBaseIntentService {

    private static final String TAG = "GCMIntentService";

    public GCMIntentService() {
        super(CommonUtilities.SENDER_ID);
    }

    @Override
    protected void onRegistered(Context context, String registrationId) {
        Log.i(TAG, "Device registered: regId = " + registrationId);
        CommonUtilities.displayMessage(context, getString(R.string.gcm_registered));
        ServerUtilities.register(context, registrationId);
    }

    @Override
    protected void onUnregistered(Context context, String registrationId) {
        Log.i(TAG, "Device unregistered");
        CommonUtilities.displayMessage(context, getString(R.string.gcm_unregistered));
        if (GCMRegistrar.isRegisteredOnServer(context)) {
            ServerUtilities.unregister(context, registrationId);
        } else {
            // This callback results from the call to unregister made on
            // ServerUtilities when the registration to the server failed.
            Log.i(TAG, "Ignoring unregister callback");
        }
    }

    @Override
    protected void onMessage(Context context, Intent intent) {
        Log.i(TAG, "Received message");

        String message = intent.getStringExtra("message");
        String url = intent.getStringExtra("url");
        CommonUtilities.displayMessage(context, message);
        // notifies user
        generateNotification(context, message, url);
    }

    @Override
    protected void onDeletedMessages(Context context, int total) {
        Log.i(TAG, "Received deleted messages notification");
        String message = getString(R.string.gcm_deleted, total);
        CommonUtilities.displayMessage(context, message);
        // notifies user
        generateNotification(context, message,"");
    }

    @Override
    public void onError(Context context, String errorId) {
        Log.i(TAG, "Received error: " + errorId);
        CommonUtilities.displayMessage(context, getString(R.string.gcm_error, errorId));
    }

    @Override
    protected boolean onRecoverableError(Context context, String errorId) {
        // log message
        Log.i(TAG, "Received recoverable error: " + errorId);
        CommonUtilities.displayMessage(context, getString(R.string.gcm_recoverable_error,
                errorId));
        return super.onRecoverableError(context, errorId);
    }

    /**
     * Issues a notification to inform the user that server has sent a message.
     */

    private static void generateNotification(Context context, String message, String url) {
        int icon = R.drawable.app_icon;
        long when = System.currentTimeMillis();
        Log.e(TAG, message);
        NotificationManager notificationManager = (NotificationManager)
                context.getSystemService(Context.NOTIFICATION_SERVICE);
        Notification notification = new Notification(icon, message, when);
        String title = context.getString(R.string.app_name);
        Intent notificationIntent;
        if(!TextUtils.isEmpty(url)){
            notificationIntent= new Intent(Intent.ACTION_VIEW);
            notificationIntent.setData(Uri.parse(url));
        }else{
            notificationIntent= new Intent(context, SplashScreen.class);
            notificationIntent.putExtra("message", message);
        }
        // set intent so it does not start a new activity
        notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
                Intent.FLAG_ACTIVITY_SINGLE_TOP);
        PendingIntent intent =
                PendingIntent.getActivity(context, 0, notificationIntent, 0);
        notification.setLatestEventInfo(context, title, message, intent);
        notification.flags |= Notification.FLAG_AUTO_CANCEL;
        notificationManager.notify(0, notification);
    }

}  

并且具有适当的权限,在清单中:

    <receiver
        android:name="com.google.android.gcm.GCMBroadcastReceiver"
        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.REGISTRATION" />

            <category android:name="com.packageappname" />
        </intent-filter>
    </receiver>

曾提到(实际上完全实现):http://www.androidhive.info/2012/10/android-push-notifications-using-google-cloud-messaging-gcm-php-and-mysql/

现在我没有收到正在推送的通知。我查了:http://developer.android.com/google/gcm/index.html

我是否必须强制实施新的 GCM 才能使其正常工作,还是我的意图服务存在问题?

【问题讨论】:

    标签: android google-cloud-messaging


    【解决方案1】:

    我在我的一个旧应用程序中使用 C2DM,并且刚刚测试了推送,它在这里工作。 有时在 wifi 家用路由器或任何路由器后面,推送不会通过,也许它只是我的路由器,但无论如何重启路由器。迁移不是一个坏主意,我做到了,花了我一周的时间来学习和做正确的事情。你看到migrate page

    【讨论】:

    • 谢谢,如何迁移?除了谷歌链接(我在问题中提到过)之外还有其他有用的链接吗?还有什么理由暂时不能使用吗??
    • 既然您了解 C2DM,您可以选择任何 GCM 教程并从头开始创建教程项目。只是谷歌它。它应该对你有用,因为它对我有用。也许该死的 regId 没有发送到您的服务器
    • 首先,你看到migrate page
    • 我收到来自服务器的成功响应,用于发送推送通知
    • 我记得这种行为,我有一些设备,有时 C2DM 可以在一个设备上工作,但不能在另一个设备上工作。这通常是因为设备以某种方式卡住,内存问题,网络问题。重启它
    猜你喜欢
    • 1970-01-01
    • 2016-11-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-05
    • 2022-01-20
    • 2016-07-18
    • 1970-01-01
    相关资源
    最近更新 更多