【问题标题】:GCM start background operations (make post to server) on notification clickGCM 在通知点击时启动后台操作(发布到服务器)
【发布时间】:2014-04-30 14:16:13
【问题描述】:

我正在为 GCM 使用 Google Play 服务库,一切都很好。 现在我在我的 notificationCompat.Builder 中实现了 2 个按钮(操作): - 一个打开的应用程序。 - 另一个删除一些未读消息:对于此操作,我将一些 dato 发布到服务器。

如何在不打开用户面前的任何活动的情况下,在点击通知操作时向我的服务器发布帖子?

非常感谢!!!!!!

这是我的代码:`Intent openIntent = new Intent(); 意图 cancelIntent = new Intent();

    if (has1Id) {
        //go details!
        openIntent = new Intent(this, NotificationActivity.class);
        openIntent.putExtra("id", ids.get(0));
        //cancell intent: start async operation in background
        cancelIntent = new Intent(this, HomeActivity.class);
        cancelIntent.putExtra("id", ids.get(0));
    }else{
        //go home!
        openIntent = new Intent(this, HomeActivity.class);
        //cancell intent: start async operation in background
        cancelIntent = new Intent(this, HomeActivity.class);
        cancelIntent.putExtra("id", ids);
    }

    PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
            openIntent, PendingIntent.FLAG_UPDATE_CURRENT);

    PendingIntent cancelNotificationIntent = PendingIntent.getActivity(this, 0,
            cancelIntent, PendingIntent.FLAG_UPDATE_CURRENT);


    long[] vibrationPattern = {0, 200, 800, 200, 100, 500};
    Uri soundUri = Uri.parse("android.resource://"+ getPackageName() + "/" + R.raw.jingle1);

    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this);
    mBuilder.setSmallIcon(R.drawable.ic_notification)
    .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher))
    .setContentTitle(title) //set title
    .setContentInfo(numNotRead) //number of unread content
    .setAutoCancel(true)
    .setContentText(msgs.get(0)+"...")
    .setVibrate(vibrationPattern)
    .setSound(soundUri)
    .setLights(Color.rgb(255, 165, 00), 800, 800)
    .setContentIntent(contentIntent).addAction(R.drawable.ic_notification, getString(R.string.open_app), contentIntent)
    .setContentIntent(cancelNotificationIntent).addAction(android.R.drawable.ic_delete, getString(R.string.mark_read), cancelNotificationIntent);

    if (has1Id) {
        NotificationCompat.BigTextStyle bigTextStyle =new NotificationCompat.BigTextStyle().bigText(msgs.get(0));
        mBuilder.setContentTitle(getString(R.string.notification_new_content));
        //get only 1 long text: the description of content
        mBuilder.setStyle(bigTextStyle);
    }else{
        NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();
        inboxStyle.setBigContentTitle(getString(R.string.notification_new_contents));
        for (int i=0; i < msgs.size(); i++) {
            inboxStyle.addLine(msgs.get(i));
        }
        mBuilder.setStyle(inboxStyle);

    }

    mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());`

【问题讨论】:

    标签: android post notifications google-play-services google-cloud-messaging


    【解决方案1】:

    您应该使用另一个类作为 cancelIntent 的 Intent 接收器,例如 BroadcastReceiver 并在没有 UI 的情况下处理该 Intent。

    public class MyIntentReceiver extends BroadcastReceiver {    
      @Override 
      public void onReceive(Context _context, Intent _intent) {
        if (_intent.getAction().equals(MY_INTENT)) {
          // TODO Handle a notification
        }
      }    
    }
    

    您也可以使用没有 UI 的 Activity 作为 Intent,但我认为以前的选项更好。

    更多信息: Android Activity with no GUI

    【讨论】:

    • 谢谢!我创建了一个广播接收器,在清单中设置所有权限,创建一个服务类,并使用日志我可以启动服务!现在我将添加邮政编码。谢谢+1!
    猜你喜欢
    • 1970-01-01
    • 2016-11-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多