0

我是Android GCM和通知事物的新手。现在,我被困在如何更新源码时,用户选项卡通知按钮PendingIntent更新sqlite

的情况是

  1. 我的服务器将发送GCM向GCM服务器
  2. GCM服务器发送消息到设备
  3. 一旦IntentService收到消息,在本地sqlite中创建新的消息记录。
  4. 2按钮,创建在通知栏新通知认为,“启动应用”,“标记为已读”和“辞退”

的“启动程序”按钮很简单,每一个教程将展示如何启动通过使用PendingIntent的新活动。但对于“马克阅读”和“解雇”我不知道如何实现它(而英语不是我的母语,我不能拿出关键字来寻找答案....所以,对不起,如果这个问题已经被问)

  • 对于“标记为已读”按钮,我希望它调用一个服务或应用程序子类来更新源码,然后取消通知,而无需启动一个活动
  • 对于“辞退”按钮,我希望它仅仅取消通知

这是有

代码
@Override 
protected void onHandleIntent(Intent intent) 
    Notification.Builder builder = new Notification.Builder(this); 
    builder.setContentTitle("Title of the notification"); 
    builder.setSmallIcon(R.drawable.notification_icon); 
    builder.setStyle(new Notification.BigTextStyle().bigText("The message that I want to show")); 
    builder.setAutoCancel(true); 

    Intent i1 = new Intent(this, MainActivity.class); 
    PendingIntent pi1 = PendingIntent.getActivity(this, UNIQUE_ID, i1, PendingIntent.FLAG_ONE_SHOT); 

    PendingIntent pi2 = null; // To mark as read, I don't how to write it yet. 
    PendingIntent pi3 = null; // To dismiss, I don't how to write it yet. 

    builder.addActon(R.drawable.launch_app_icon, "Launch app", pi1); 
    builder.addActon(R.drawable.mark_as_read_icon, "Mark as read", pi2); 
    builder.addActon(R.drawable.dismiss_icon, "Dismiss", pi3); 

    Notification notification; 
    if (DeviceHelper.getAndroidSDK() < 16) { 
     notification = builder.getNotification(); 
    } else { 
     notification = builder.build(); 
    } 

    NotificationManager notificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE); 
    notificationManager.notify(UNIQUE_ID, notification);   
} 

我也试图设置PI2是这样

Intent i2 = new Intent(MyApp.USER_DISMISS_NOTIFICATION); 
PendingIntent pi2 = PendingIntent.getBroadcast(this, UNIQUE_ID, i2, PendingIntent.FLAG_ON_SHOT); 

再听听本地广播在Application子类

public final class MyApp extends Application { 
    public static final USER_DISMISS_NOTIFICATION = "UserDismissNotification"; 

    public void onCreate() { 
     super.onCreate(); 

     LocalBroadcastManager.getInstance(this).registerReceiver(new BroadcastReceiver() { 
      @Override 
      public void onReceive(Context context, Intent intent) { 
       // Local broadcast received, update database 
      } 
     }, new IntentFilter(USER_DISMISS_NOTIFICATION)); 
    } 
} 

但广播接收器的的onReceive永远不会被调用。

任何建议,欢迎,谢谢!

回答

0

我做它的工作原理如下

A.创建广播接收器

public class NotificationReceiver extends BroadcastReceiver { 
    @Override 
    public void onReceive(Context context, Intent intent) { 
     Log.i("Dev", "Received"); 
    } 
} 

B.注册接收到Application对象**

<application 
    android:name="com.example.app" 
    android:icon="@drawable/ic_launcher" 
    android:label="@string/application_name" 
    android:theme="@android:style/Theme.Holo.NoActionBar" 
    android:hardwareAccelerated="true" > 

    <activity 
     android:name="com.example.app.MainActivity" /> 

    <receiver android:name="com.example.NotificationReceiver" /> 
</application> 

C.创建挂起的意图如下,并添加它要通知

Intent i = new Intent(this, NotificationReceiver.class); 
PendingIntent pi = PendingIntent.getActivity(this, UNIQUE_ID, i, PendingIntent.FLAG_ONE_SHOT); 

notification.addAction(R.drawable.button_icon, "Do something", pi); 

现在,当用户选择一个按钮时,NotifiationReceiver的onReceive将被调用,然后我可以更新数据库,启动活动或基于意向包的参数。

希望这有助于

+0

如果你想发送意图广播接收器,你应该使用PendingIntent.getBroadcast方法,而不是PendingIntent.getActivity – 2015-10-12 15:27:04