Android平台中的推送通知

Android平台中的推送通知,android,push,alerts,Android,Push,Alerts,我想写一个应用程序,从服务器接收推送警报。我找到了一些方法来做这件事 SMS-拦截传入的SMS并从服务器启动拉取 定期轮询服务器 每个都有自己的局限性。短信-不保证到达时间。轮询可能会耗尽电池电量 你有更好的建议吗?非常感谢。恐怕您已经找到了两种可能的方法。至少在最初,谷歌打算实现一个gchatapi,您可以使用它来实现推/拉实现。不幸的是,这个库被安卓1.0删除了。或者 3)与服务器保持连接,每隔几分钟发送一次Keep alives,服务器就可以立即推送消息。这就是Gmail、Google T

我想写一个应用程序,从服务器接收推送警报。我找到了一些方法来做这件事

  • SMS-拦截传入的SMS并从服务器启动拉取
  • 定期轮询服务器
  • 每个都有自己的局限性。短信-不保证到达时间。轮询可能会耗尽电池电量


    你有更好的建议吗?非常感谢。

    恐怕您已经找到了两种可能的方法。至少在最初,谷歌打算实现一个gchatapi,您可以使用它来实现推/拉实现。不幸的是,这个库被安卓1.0删除了。

    或者

    3)与服务器保持连接,每隔几分钟发送一次Keep alives,服务器就可以立即推送消息。这就是Gmail、Google Talk等的工作原理

    (从我对一个类似问题的回答中交叉发布-)

    我最近开始使用MQTT for Android来做这类事情(即推送通知,不是短信而是数据驱动的,几乎是即时消息传递,不是轮询,等等)

    我有一篇关于这方面背景信息的博文,以防对你有所帮助


    (注意:MQTT是一种IBM技术,我应该指出我为IBM工作。)

    我不知道这是否仍然有用。我通过在


    尽管在服务中这样做不会阻止android关闭它,并杀死侦听器线程。

    您可以使用Xtify()-他们有一个推送通知Web服务,可以与SDK一起使用。它是免费的,到目前为止,它对我来说运行得非常好。

    有一项新的开源工作,旨在开发一个基于Meteor web服务器的Android推送通知Java库。您可以在上查看它,在那里可以找到指向Meteor和项目GitHub存储库的链接。我们需要开发者,所以请传播信息

    谷歌的官方答案是(不推荐)(不推荐)


    它将在Android>=2.2(在有Play Store的手机上)上工作。

    我建议同时使用SMS和HTTP。如果用户未登录,请向其手机发送短信,通知他们有消息等待

    这就是爱立信实验室服务的工作原理:

    如果您自己实现了这一点,那么棘手的部分是在用户看不到的情况下删除传入的SMS。或者如果他们在你的案例中看到这一点也没关系

    看起来这样行得通:


    是的,编写这样的代码可能会很危险,而且你可能会毁掉某人的生命,因为你的应用程序删除了一条它不应该有的短信。

    我对Android推送通知的理解/经验是:

  • C2DM-如果你的目标android平台是2.2+,那就试试吧。只有一个问题,设备用户必须始终使用Google帐户登录才能获得消息

  • MQTT-基于发布/订阅的方法,需要设备的主动连接,如果不合理实施,可能会耗尽电池电量

  • -由于社区支持有限,从长远来看可能不太好

  • 编辑:于2013年11月25日添加

    -谷歌说

    对于3.0之前的设备,这要求用户在其移动设备上设置Google帐户。运行安卓4.0.4或更高版本的设备不需要谷歌帐户*


    Android云到设备消息框架

    重要提示:C2DM已于2012年6月26日被正式弃用。这意味着C2DM已停止接受新用户和配额请求。不会向C2DM添加任何新功能。然而,使用C2DM的应用程序将继续工作。鼓励现有的C2DM开发者迁移到新版本的C2DM,称为Android的Google云消息(GCM)。有关更多信息,请参阅C2DM到GCM迁移文档。开发人员必须使用GCM进行新的开发

    请查看以下链接:


    C2DM:您的应用程序用户必须拥有gmail帐户

    MQTT:当您的连接达到1024时,它将停止工作,因为它使用了linux的“选择模型”


    android有一个免费的推送服务和api,你可以试试看:

    谷歌C2DM现在贬值了,为此,你必须使用新的服务GCM(谷歌云消息)。有关documantation,请参见

    您可以使用谷歌云消息服务,或者,它是免费且易于使用的。此外,您还可以使用第三方推送服务器,这样可以为您提供更大的灵活性

    有很多第三方服务器,如Xtify。。。它不仅允许在Android上发送,还允许在iOs、Windows Phone上发送…

    这里我写了几个步骤,介绍如何从头开始获取注册表和通知

  • 在谷歌云上创建/注册应用程序
  • 通过开发安装云SDK
  • 为GCM配置项目
  • 获取设备注册ID
  • 发送推送通知
  • 接收推送通知
  • 您可以在下面的URL链接中找到完整的教程

    代码剪切以获取注册ID(推送通知的设备令牌)

    为GCM配置项目


    更新AndroidManifest文件 为了在我们的项目中启用GCM,我们需要在清单文件中添加一些权限 转到AndroidManifest.xml并添加以下代码 添加权限

    <uses-permission android:name="android.permission.INTERNET”/>
    <uses-permission android:name="android.permission.GET_ACCOUNTS" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    
    <uses-permission android:name="android.permission.VIBRATE" />
    
    <uses-permission android:name=“.permission.RECEIVE" />
    <uses-permission android:name=“<your_package_name_here>.permission.C2D_MESSAGE" />
    <permission android:name=“<your_package_name_here>.permission.C2D_MESSAGE"
            android:protectionLevel="signature" />
    
    更新OnCreate和OnResume方法

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.activity_launch);
         context = getApplicationContext();
             if (checkPlayServices()) 
         {
                gcm = GoogleCloudMessaging.getInstance(this);
                regid = getRegistrationId(context);
    
                if (regid.isEmpty())
                {
                    registerInBackground();
                }
                else
                {
                Log.d(TAG, "No valid Google Play Services APK found.");
                }
          }
     }
    
    @Override protected void onResume()
    {
           super.onResume();       checkPlayServices();
    }
    
    
    # Implement GCM Required methods (Add below methods in LaunchActivity)
    
    private boolean checkPlayServices() {
            int resultCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
            if (resultCode != ConnectionResult.SUCCESS) {
                if (GooglePlayServicesUtil.isUserRecoverableError(resultCode)) {
                    GooglePlayServicesUtil.getErrorDialog(resultCode, this,
                            PLAY_SERVICES_RESOLUTION_REQUEST).show();
                } else {
                    Log.d(TAG, "This device is not supported - Google Play Services.");
                    finish();
                }
                return false;
            }
            return true;
     }
    
    private String getRegistrationId(Context context) 
    {
       final SharedPreferences prefs = getGCMPreferences(context);
       String registrationId = prefs.getString(PROPERTY_REG_ID, "");
       if (registrationId.isEmpty()) {
           Log.d(TAG, "Registration ID not found.");
           return "";
       }
       int registeredVersion = prefs.getInt(PROPERTY_APP_VERSION, Integer.MIN_VALUE);
       int currentVersion = getAppVersion(context);
       if (registeredVersion != currentVersion) {
            Log.d(TAG, "App version changed.");
            return "";
        }
        return registrationId;
    }
    
    private SharedPreferences getGCMPreferences(Context context) 
    {
        return getSharedPreferences(LaunchActivity.class.getSimpleName(),
                    Context.MODE_PRIVATE);
    }
    
    private static int getAppVersion(Context context) 
    {
         try 
         {
             PackageInfo packageInfo = context.getPackageManager()
                        .getPackageInfo(context.getPackageName(), 0);
                return packageInfo.versionCode;
          } 
          catch (NameNotFoundException e) 
          {
                throw new RuntimeException("Could not get package name: " + e);
          }
    }
    
    
    private void registerInBackground() 
    {     new AsyncTask() {
         Override
         protected Object doInBackground(Object... params) 
         {
              String msg = "";
              try 
              {
                   if (gcm == null) 
                   {
                            gcm = GoogleCloudMessaging.getInstance(context);
                   }
                   regid = gcm.register(SENDER_ID);               Log.d(TAG, "########################################");
                   Log.d(TAG, "Current Device's Registration ID is: "+msg);     
              } 
              catch (IOException ex) 
              {
                  msg = "Error :" + ex.getMessage();
              }
              return null;
         }     protected void onPostExecute(Object result) 
         { //to do here };
      }.execute(null, null, null);
    }
    

    注意:请保存注册密钥,这对于向GCM发送PN消息很重要 此外,这对所有设备都是唯一的,只有GCM会使用它发送推送通知

    接收推送通知 添加GCM广播接收器类

    因为我们已经在清单文件中声明了“GcmBroadcastReceiver.java”,所以让我们创建这个类 以这种方式更新接收器类代码

    public class GcmBroadcastReceiver extends WakefulBroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) 
        {        ComponentName comp = new ComponentName(context.getPackageName(),
                    GcmIntentService.class.getName());        startWakefulService(context, (intent.setComponent(comp)));
            setResultCode(Activity.RESULT_OK);
            Toast.makeText(context, “wow!! received new push notification", Toast.LENGTH_LONG).show();
        }
    }
    
    public class GcmIntentService extends IntentService
    {     public static final int NOTIFICATION_ID = 1;     private NotificationManager mNotificationManager;     private final static String TAG = "GcmIntentService";     public GcmIntentService() {
         super("GcmIntentService");     
         }     @Override
         protected void onHandleIntent(Intent intent) {
              Bundle extras = intent.getExtras();
              Log.d(TAG, "Notification Data Json :" + extras.getString("message"));
    
              GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);
              String messageType = gcm.getMessageType(intent);          if (!extras.isEmpty()) {          if (GoogleCloudMessaging.MESSAGE_TYPE_SEND_ERROR
                   .equals(messageType)) {
                   sendNotification("Send error: " + extras.toString());
              } else if (GoogleCloudMessaging.MESSAGE_TYPE_DELETED
              .equals(messageType)) {
              sendNotification("Deleted messages on server: "
              + extras.toString());          // If it's a regular GCM message, do some work.
              } else if (GoogleCloudMessaging.MESSAGE_TYPE_MESSAGE
              .equals(messageType)) {
              // This loop represents the service doing some work.
              for (int i = 0; i < 5; i++) {
                   Log.d(TAG," Working... " + (i + 1) + "/5 @ "
                   + SystemClock.elapsedRealtime());               try {
                        Thread.sleep(5000);
                   } catch (InterruptedException e) {
                   }
                 }
                 Log.i(TAG, "Completed work @ " + SystemClock.elapsedRealtime());
                 sendNotification(extras.getString("message"));
               }
            }        // Release the wake lock provided by the WakefulBroadcastReceiver.
            GcmBroadcastReceiver.completeWakefulIntent(intent);
         }     // Put the message into a notification and post it.
         // This is just one simple example of what you might choose to do with
         // a GCM message.
         private void sendNotification(String msg) {          mNotificationManager = (NotificationManager) this
              .getSystemService(Context.NOTIFICATION_SERVICE);
              PendingIntent contentIntent = PendingIntent.getActivity(this, 0,          new Intent(this, LaunchActivity.class), 0);
    
              NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(          this)
              .setSmallIcon(R.drawable.icon)
              .setContentTitle("Ocutag Snap")
              .setStyle(new NotificationCompat.BigTextStyle().bigText(msg))
              .setContentText(msg)
              .setDefaults(Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE);
    
              mBuilder.setContentIntent(contentIntent);          mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
         }
    }
    
    添加GCM服务类

    因为我们已经在清单文件中声明了“GcmBroadcastReceiver.java”,所以让我们创建这个类 更新
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.activity_launch);
         context = getApplicationContext();
             if (checkPlayServices()) 
         {
                gcm = GoogleCloudMessaging.getInstance(this);
                regid = getRegistrationId(context);
    
                if (regid.isEmpty())
                {
                    registerInBackground();
                }
                else
                {
                Log.d(TAG, "No valid Google Play Services APK found.");
                }
          }
     }
    
    @Override protected void onResume()
    {
           super.onResume();       checkPlayServices();
    }
    
    
    # Implement GCM Required methods (Add below methods in LaunchActivity)
    
    private boolean checkPlayServices() {
            int resultCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
            if (resultCode != ConnectionResult.SUCCESS) {
                if (GooglePlayServicesUtil.isUserRecoverableError(resultCode)) {
                    GooglePlayServicesUtil.getErrorDialog(resultCode, this,
                            PLAY_SERVICES_RESOLUTION_REQUEST).show();
                } else {
                    Log.d(TAG, "This device is not supported - Google Play Services.");
                    finish();
                }
                return false;
            }
            return true;
     }
    
    private String getRegistrationId(Context context) 
    {
       final SharedPreferences prefs = getGCMPreferences(context);
       String registrationId = prefs.getString(PROPERTY_REG_ID, "");
       if (registrationId.isEmpty()) {
           Log.d(TAG, "Registration ID not found.");
           return "";
       }
       int registeredVersion = prefs.getInt(PROPERTY_APP_VERSION, Integer.MIN_VALUE);
       int currentVersion = getAppVersion(context);
       if (registeredVersion != currentVersion) {
            Log.d(TAG, "App version changed.");
            return "";
        }
        return registrationId;
    }
    
    private SharedPreferences getGCMPreferences(Context context) 
    {
        return getSharedPreferences(LaunchActivity.class.getSimpleName(),
                    Context.MODE_PRIVATE);
    }
    
    private static int getAppVersion(Context context) 
    {
         try 
         {
             PackageInfo packageInfo = context.getPackageManager()
                        .getPackageInfo(context.getPackageName(), 0);
                return packageInfo.versionCode;
          } 
          catch (NameNotFoundException e) 
          {
                throw new RuntimeException("Could not get package name: " + e);
          }
    }
    
    
    private void registerInBackground() 
    {     new AsyncTask() {
         Override
         protected Object doInBackground(Object... params) 
         {
              String msg = "";
              try 
              {
                   if (gcm == null) 
                   {
                            gcm = GoogleCloudMessaging.getInstance(context);
                   }
                   regid = gcm.register(SENDER_ID);               Log.d(TAG, "########################################");
                   Log.d(TAG, "Current Device's Registration ID is: "+msg);     
              } 
              catch (IOException ex) 
              {
                  msg = "Error :" + ex.getMessage();
              }
              return null;
         }     protected void onPostExecute(Object result) 
         { //to do here };
      }.execute(null, null, null);
    }
    
    public class GcmBroadcastReceiver extends WakefulBroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) 
        {        ComponentName comp = new ComponentName(context.getPackageName(),
                    GcmIntentService.class.getName());        startWakefulService(context, (intent.setComponent(comp)));
            setResultCode(Activity.RESULT_OK);
            Toast.makeText(context, “wow!! received new push notification", Toast.LENGTH_LONG).show();
        }
    }
    
    public class GcmIntentService extends IntentService
    {     public static final int NOTIFICATION_ID = 1;     private NotificationManager mNotificationManager;     private final static String TAG = "GcmIntentService";     public GcmIntentService() {
         super("GcmIntentService");     
         }     @Override
         protected void onHandleIntent(Intent intent) {
              Bundle extras = intent.getExtras();
              Log.d(TAG, "Notification Data Json :" + extras.getString("message"));
    
              GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);
              String messageType = gcm.getMessageType(intent);          if (!extras.isEmpty()) {          if (GoogleCloudMessaging.MESSAGE_TYPE_SEND_ERROR
                   .equals(messageType)) {
                   sendNotification("Send error: " + extras.toString());
              } else if (GoogleCloudMessaging.MESSAGE_TYPE_DELETED
              .equals(messageType)) {
              sendNotification("Deleted messages on server: "
              + extras.toString());          // If it's a regular GCM message, do some work.
              } else if (GoogleCloudMessaging.MESSAGE_TYPE_MESSAGE
              .equals(messageType)) {
              // This loop represents the service doing some work.
              for (int i = 0; i < 5; i++) {
                   Log.d(TAG," Working... " + (i + 1) + "/5 @ "
                   + SystemClock.elapsedRealtime());               try {
                        Thread.sleep(5000);
                   } catch (InterruptedException e) {
                   }
                 }
                 Log.i(TAG, "Completed work @ " + SystemClock.elapsedRealtime());
                 sendNotification(extras.getString("message"));
               }
            }        // Release the wake lock provided by the WakefulBroadcastReceiver.
            GcmBroadcastReceiver.completeWakefulIntent(intent);
         }     // Put the message into a notification and post it.
         // This is just one simple example of what you might choose to do with
         // a GCM message.
         private void sendNotification(String msg) {          mNotificationManager = (NotificationManager) this
              .getSystemService(Context.NOTIFICATION_SERVICE);
              PendingIntent contentIntent = PendingIntent.getActivity(this, 0,          new Intent(this, LaunchActivity.class), 0);
    
              NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(          this)
              .setSmallIcon(R.drawable.icon)
              .setContentTitle("Ocutag Snap")
              .setStyle(new NotificationCompat.BigTextStyle().bigText(msg))
              .setContentText(msg)
              .setDefaults(Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE);
    
              mBuilder.setContentIntent(contentIntent);          mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
         }
    }