首页 文章

用户打开通知时,Android GCM显示自定义意图

提问于
浏览
0

我正在尝试构建使用GCM推送通知实现的新闻应用程序 . Senario基于以下内容:

当用户点击通知时,应该打开包含通知正文的服装意图 .

我已经弄清楚了上述的大部分情况,但我还有一个问题:

服装意图显示最新通知的消息

我的代码是:

@Override
   protected void onMessage(Context context, Intent intent) {
        String message = intent.getExtras().getString("price");

         displayMessage(context, message);
         // notifies user
        generateNotification(context, message);
     }

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, NotificationActivity.class);
// set intent so it does not start a new activity
Bundle b = new Bundle();
b.putString("message", message);
notificationIntent.putExtras(b);

notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK|
        Intent.FLAG_ACTIVITY_SINGLE_TOP);
 UNIQUE_INT_PER_CALL ++;

PendingIntent contentIntent = PendingIntent.getActivity(
        context,
        UNIQUE_INT_PER_CALL, 
        notificationIntent, // add this
        PendingIntent.FLAG_UPDATE_CURRENT);

notification.setLatestEventInfo(context, title, message, contentIntent);
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;
Random generator = new Random(); 
int r = generator.nextInt();
notificationManager.notify(r, notification);      
System.exit(0);

}

我在这里如何阅读服装意图中的消息

Bundle b = getIntent().getExtras();
     String message =  null ;
     if (b != null) {
        message = b.getString("message");
        //Log.i(null,"message  = "+message);
     }

我是android开发的新手,上面的代码是很多例子的结果(也许我有一些无用的代码)

1 回答

  • 0

    你应该读这页> https://developers.google.com/cloud-messaging/android/start

    private void sendNotification(String message) {
        Intent intent = new Intent(this, YOURCLASS.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
        PendingIntent.FLAG_ONE_SHOT);
    
        Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
                .setSmallIcon(R.drawable.ic_park_notification)
                .setContentTitle("Parkuest Message")
                .setContentText(message)
                .setAutoCancel(true)
                .setSound(defaultSoundUri)
                .setContentIntent(pendingIntent);
    
        NotificationManager notificationManager =
                (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    
        notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
    }
    

    将其添加到您的方法中 .

    @Override
    public void onMessageReceived(String from, Bundle data)
        sendNotification(message)
     }
    

相关问题