【问题标题】:android: notification doesn't do anythingandroid:通知没有做任何事情
【发布时间】:2014-12-05 11:04:42
【问题描述】:

嘿,我正在制作一个用户可以相互发送 gcm 消息的应用程序 该应用程序运行良好,当我发送 gcm 消息时,接收者将其作为通知 当接收者点击它时,它会打开 homepage.class 但问题是 当他单击通知时,广播接收者没有收到它并且没有对 EditText 进行更改,另一方面,如果接收者在收到消息时正在使用 homepage.class,则对 EditText 进行更改(工作) 可能是什么问题呢 ??? 我的 GCMIntentService 中的一些方法

 @Override
  private static void generateNotification(Context context, String message) {
        int icon = R.drawable.ic_launcher;
        long when = System.currentTimeMillis();
        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 = new Intent(context, HomePage.class);
        // 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;
        // Play default notification sound
        notification.defaults |= Notification.DEFAULT_SOUND;

        // Vibrate if vibrate is enabled
        notification.defaults |= Notification.DEFAULT_VIBRATE;
        notificationManager.notify(0, notification); 
    }


    @Override
    protected void onMessage(Context context, Intent intent) {

        String message = intent.getExtras().getString("message");
        // notifies user
        aController.displayMessageOnScreen(context, message);
        generateNotification(context, message);

    }

 void displayMessageOnScreen(Context context, String message) {

        Intent intent = new Intent("com.ms.gp.wefamily.DISPLAY_MESSAGE");
        intent.putExtra("message", message);
        // Send Broadcast to Broadcast receiver with message
        context.sendBroadcast(intent);   
    } 

这是我的 BroadcastReceiver 的代码

@Override 
private final BroadcastReceiver mHandleMessageReceiver = new BroadcastReceiver() {

    public void onReceive(Context context, Intent intent) { 
        String newMessage = intent.getExtras().getString("message");
        // Display message on the screen
        Familyname.setText(newMessage);                  
    }
};

如果有人知道答案,请告诉我(抱歉英语不好)

【问题讨论】:

  • 这个问题我不清楚,为什么要通过点击通知来触发broadcastreceiver???
  • 另外,你有一个名为 Familiyname 的类???还是应该是一个字段...
  • Familyname 是一个 EditText 对象,如果广播接收器未触发通知应该是什么?
  • 广播接收器在收到来自 GCM 的消息时触发,onclick 部分类似于此线程stackoverflow.com/questions/15155804/…

标签: java android android-intent notifications google-cloud-messaging


【解决方案1】:

确保在其父活动中注册接收器。一旦它被正确注册,让它处理收到的包。注意发送的值标签。

【讨论】:

    猜你喜欢
    • 2012-09-07
    • 2016-10-02
    • 2013-09-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多