【问题标题】:Android: Push NotificationAndroid:推送通知
【发布时间】:2013-09-11 09:28:50
【问题描述】:

我正在开发一个应用程序。在该应用程序中,我使用 GCM(推送通知)。

我得到了这个Link 1的帮助。

我成功实现了客户端代码。

现在要发送通知,我使用以下链接:

Link 2

但问题是我可以收到通知,但是当我点击它时什么都没有发生。

我应该怎么做,如果用户点击通知网页或更新页面等打开。

我使用了与该链接中给出的相同代码。目前尚未进行任何更改。

我在我的应用程序中间。 所以请指导我并提出您宝贵的建议。

【问题讨论】:

    标签: android push-notification android-c2dm android-notifications google-cloud-messaging


    【解决方案1】:

    这个函数在你的主要活动中被调用。

    public void registerClient() {
    
            try {
                // Check that the device supports GCM (should be in a try / catch)
                GCMRegistrar.checkDevice(viewLogin);
    
                // Check the manifest to be sure this app has all the required
                // permissions.
                GCMRegistrar.checkManifest(viewLogin);
    
                // Get the existing registration id, if it exists.
                regId = GCMRegistrar.getRegistrationId(viewLogin);
    
                if (regId.equals("")) {
    
                    registrationStatus = "Registering...";
    
                    // register this device for this project
                    GCMRegistrar.register(viewLogin, GCMIntentService.PROJECT_ID);
                    regId = GCMRegistrar.getRegistrationId(viewLogin);
    
                    registrationStatus = "Registration Acquired";
    
                    // This is actually a dummy function. At this point, one
                    // would send the registration id, and other identifying
                    // information to your server, which should save the id
                    // for use when broadcasting messages.
    
                } else {
    
                    registrationStatus = "Already registered";
    
                }
                Log.d(TAG, regId);
                sendRegistrationToServer();
            } catch (Exception e) {
    
                e.printStackTrace();
                registrationStatus = e.getMessage();
    
            }
    
            Log.d(TAG, registrationStatus);
    
    
        }
    

    公共类 GCMIntentService 扩展 GCMBaseIntentService {

        public static final String PROJECT_ID = "566655788";
        private static final String TAG = "GCMIntentService";
        ModelNotificationMessage modelNotificationMessage;
    
        public GCMIntentService() {
            super(PROJECT_ID);
            Log.d(TAG, "GCMIntentService init");
        }
    
        @Override
        protected void onError(Context ctx, String sError) {
            // TODO Auto-generated method stub
            Log.d(TAG, "Error: " + sError);
    
        }
    
        @Override
        protected void onMessage(Context ctx, Intent intent) {
    
            Log.d(TAG, "Message Received");
    
            String message = intent.getStringExtra("message");
            try {
                modelNotificationMessage = JsonParserNotificationMessage
                        .parserString(message);
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            Log.d(TAG, "Message Received" + message);
    
            sendNotification(message);
            Intent broadcastIntent = new Intent();
            broadcastIntent.setAction("GCM_RECEIVED_ACTION");
    
            broadcastIntent.putExtra("gcm", message);
    
            ctx.sendBroadcast(broadcastIntent);
    
        }
    
        private void sendNotification(String message) {
            // this
            String ns = Context.NOTIFICATION_SERVICE;
            NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);
    
            int icon = R.drawable.notification;
            CharSequence tickerText = message; // ticker-text
            long when = System.currentTimeMillis();
            Context context = getApplicationContext();
            CharSequence contentTitle = modelNotificationMessage.getKey();
            CharSequence contentText = message;
            Intent notificationIntent = null;
    
            int NOTIFICATION_ID = 9999;
            try {
    
                    NOTIFICATION_ID = CommonVariable.notification_Limit;
                    notificationIntent = new Intent(this, ViewLimit.class);
    
                    contentText = "Limit received for " + modelAgents.getName()
                            + ".";
                    tickerText = "Limit received for " + modelAgents.getName()
                            + ".";
    
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            // and this
            PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
                    notificationIntent, 0);
            Notification notification = new Notification(icon, tickerText, when);
            // Play default notification sound
            notification.defaults |= Notification.DEFAULT_ALL;
            notification.setLatestEventInfo(context, contentTitle, contentText,
                    contentIntent);
            mNotificationManager.notify(NOTIFICATION_ID, notification);
        }
    
        @Override
        protected void onRegistered(Context ctx, String regId) {
            // TODO Auto-generated method stub
            // send regId to your server
            Log.d(TAG, regId);
    
        }
    
        @Override
        protected void onUnregistered(Context ctx, String regId) {
            // TODO Auto-generated method stub
            // send notification to your server to remove that regId
    
        }
    
    }
    

    //json解析器类

    public class JsonParserNotificationMessage {
    
        private static final String KEY = "Key";
        private static final String BODY = "Body";
    
        public static ModelNotificationMessage parserString(String jsonStrng)
                throws JSONException {
            JSONObject jObject = new JSONObject(jsonStrng);
            ModelNotificationMessage modelNotificationMessage = new ModelNotificationMessage();
            if (jObject != null) {
                modelNotificationMessage.setKey(jObject.getString(KEY));
                modelNotificationMessage.setBody(jObject.getString(BODY));
            }
            return modelNotificationMessage;
        }
    }
    

    希望对你有用... 如果您有任何疑问,请告诉我。此代码在我的应用中成功运行。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-06-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-31
      • 2011-09-10
      • 2014-04-29
      相关资源
      最近更新 更多