首页 文章

android GCM推送通知

提问于
浏览
3

我已经成功设置了在Android应用程序上通过phonegap运行GCM的代码 . 我已设法保护手机注册ID,并能够在PHP脚本中使用此ID向应用程序发送消息 . 我唯一的问题是,当应用程序打开时,消息显示为javascript警报,我希望将消息发送到手机,但不会在顶部通知栏上显示图标 .

**>确实可以在Android设备的顶部栏上显示图标

有谁知道Phonegap的GCM插件是否能够这样做?**

我使用过C2DM-Phonegap . https://github.com/marknutter/GCM-Cordova

请帮帮我朋友......

4 回答

  • 2

    您正在使用的库指定收到消息时需要执行的操作 . 您可以修改其代码以执行所需操作,也可以滚动自己的GCMIntentService .

    请参考以下链接:

    https://github.com/phonegap-build/PushPlugin

    https://build.phonegap.com/plugins/324

  • 0

    您正在使用的库指定收到消息时需要执行的操作 . 您可以修改其代码以执行所需操作,也可以滚动自己的GCMIntentService .

  • 4

    最快的方法是将以下代码添加到onMessage函数中的GCMIntentService.java中 . 您使用的GCM插件只接收GCM消息,但您必须自己触发通知 . 您还可以尝试使用cordova插件进行状态栏通知 .

    protected void onMessage(Context context, Intent intent) {
    Log.d(TAG, "onMessage - context: " + context);
    
    // Extract the payload from the message
    Bundle extras = intent.getExtras();
    if (extras != null) {
        String message = extras.getString("message");
        String title = extras.getString("title");
    
        Notification notif = new Notification(R.drawable.ic_launcher, message, System.currentTimeMillis() );
        notif.flags = Notification.FLAG_AUTO_CANCEL;
        notif.defaults |= Notification.DEFAULT_SOUND;
        notif.defaults |= Notification.DEFAULT_VIBRATE;
    
        Intent notificationIntent = new Intent(context, SomeActivity.class);
        notificationIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
        PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
    
        notif.setLatestEventInfo(context, "Title of notification", message, contentIntent);
        String ns = Context.NOTIFICATION_SERVICE;
        NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(ns);
        mNotificationManager.notify(1, notif);
    
  • 0

    请检查此代码

    private static void generateNotification(Context context,String message){ int icon = R.drawable.ic_launcher;

    // here is your icon drawable instead of 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.getApplicationContext(), devicephp.class);
        // set intent so it does not start a new activity
    
        notificationIntent.putExtra("msg", message);
        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;
        notificationManager.notify(0, notification);
    
    
    }
    

相关问题