【问题标题】:Push Notifications - Android推送通知 - Android
【发布时间】:2016-06-12 17:41:33
【问题描述】:

我正在做一个涉及 Web 界面的小项目,该界面可以将信息发送到我的 android 应用程序,该应用程序将显示推送通知等信息。

但事情是这样的,我对如何做到这一点有点困惑。至于我必须采取什么步骤。

所以我有一个 HTML 格式的 Web 界面,其中有一个用于通知标题、内容和提交按钮的文本字段。我希望当用户单击提交按钮时,网页会将标题和内容字段中的文本发送到我的 android 应用程序,然后应用程序会将它们显示为推送通知。

到目前为止,在应用程序上,当您单击设备上的按钮时,它只会在操作栏上显示通知。这非常适合测试,但您最好通过 Web 界面编写通知。

我的应用测试推送通知代码:

button.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Intent intent = new Intent();
        PendingIntent pIntent = PendingIntent.getActivity(MainActivity.this, 0, intent, 0);

        // TODO: Make this accessible to exterior projects, such as web interface.
        Notification notification = new Notification.Builder(MainActivity.this)
                .setTicker("Notification")
                .setContentTitle("Important Message")
                .setContentText("This is an example of a push notification using a Navigation Manager")
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentIntent(pIntent)
                .build();

        notification.flags = Notification.FLAG_AUTO_CANCEL;

        NotificationManager nManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

        nManager.notify(0, notification);
    }
});

如果有人能帮我一把,将不胜感激。

【问题讨论】:

  • 网页界面可以在桌面或移动设备上使用
  • 如果它可以在移动浏览器上运行,那么你可以在 上搜索,这样你就不必实现 GCM

标签: java android web-services web push-notification


【解决方案1】:

你是对的,到目前为止,通知栏做得很好,现在你需要的是通知服务,而谷歌为我们提供了类似的东西......

这是如何工作的??

看看下面的图片,

您需要在 google 服务中注册您的 android 应用程序,并且您的 web 界面将需要一个 id,因此每次您想将某些内容推送到 android 时,您的 web 界面都会将其推送到具有 id 的 google 服务器该应用程序,然后谷歌(无论如何)将对您的应用程序进行本地化,即使它没有运行,他们也会收到通知,

在幕后,您必须做几件事,但没有比从 NASA 发射火箭更重要的事情了。

我会建议看看一些tutorials 为了开始注册您的应用,请获取 api 密钥等。

【讨论】:

    【解决方案2】:

    这是 github 中的一个很好的来源,它展示了如何在你的 android 应用中添加推送通知服务

    github.com/rana01645/android-push-notification

    首先阅读完整文档

    如何在 android studio 的 android 应用程序中添加推送通知 – Android 开发者(部分 – 1 与 firebase 连接)~http://androidrace.com/2016/12/08/how-to-add-push-notification-in-android-application-from-android-studio-android-developer-part-1-connect-with-firebase/

    如何在 android studio 的 android 应用程序中添加推送通知 – Android 开发者(部分 – 2 使用服务器)~http://androidrace.com/2017/01/05/how-to-add-push-notification-in-android-application-from-android-studio-android-developer-part-2-working-with-server/

    然后您可以使用 html 从您的服务器发送推送通知

    【讨论】:

      【解决方案3】:
      public class Uyarilar extends BroadcastReceiver {
      
      @Override
      public void onReceive(Context context, Intent arg1) {
          Date currentTime = Calendar.getInstance().getTime();
      
              showNotification(context);
      }
      
      private void showNotification(Context context) {
          PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
                  new Intent(context, MainActivity.class), 0);
      
          NotificationCompat.Builder mBuilder =
                  new NotificationCompat.Builder(context)
                          .setSmallIcon(R.drawable.presta)
                          .setContentTitle("Saat 9:00")
                          .setContentText("Mesai saatiniz başlamıştır Lütfen harakete geçiniz!");
          mBuilder.setContentIntent(contentIntent);
          mBuilder.setDefaults(Notification.DEFAULT_SOUND);
          mBuilder.setAutoCancel(true);
          NotificationManager mNotificationManager =
                  (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
          mNotificationManager.notify(1, mBuilder.build());
      
      }
      }
      

      然后打电话

        private void setNotification() {
            Calendar calNow = Calendar.getInstance();
            Calendar calSet = (Calendar) calNow.clone();
            calSet.set(Calendar.HOUR_OF_DAY, 9);
            calSet.set(Calendar.MINUTE, 00);
            calSet.set(Calendar.SECOND, 0);
            calSet.set(Calendar.MILLISECOND, 0);
            if (calSet.compareTo(calNow) <= 0) {
                calSet.add(Calendar.DATE, 1);
            }
      
      
                   Date currentTime = Calendar.getInstance().getTime();
                   Intent intent = new Intent(getBaseContext(), Uyarilar.class);
                   PendingIntent pendingIntent = PendingIntent.getBroadcast(getBaseContext(), REQUEST_CODE, intent, 0);
                   AlarmManager alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
                   alarmManager.set(AlarmManager.RTC_WAKEUP, calSet.getTimeInMillis(), pendingIntent);
      
      
        }
      

      和 创建 setNotification();

      【讨论】:

        【解决方案4】:

        此方法推送通知

        public void testMessage (String message , Intent  intent){
        
         PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 , intent,
                    PendingIntent.FLAG_ONE_SHOT);
        
        
            String channelId = "some_channel_id";
            Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
            android.support.v4.app.NotificationCompat.Builder notificationBuilder =
                    new android.support.v4.app.NotificationCompat.Builder(this, channelId)
                            .setSmallIcon(R.mipmap.ic_launcher_round)
                            .setContentTitle(getString(R.string.app_name))
                            .setContentText(message)
                            .setAutoCancel(true)
                            .setSound(defaultSoundUri)
                            .setBadgeIconType(android.support.v4.app.NotificationCompat.BADGE_ICON_SMALL)
                            .setContentIntent(pendingIntent);
        
            NotificationManager notificationManager =
                    (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        
            // Since android Oreo notification channel is needed.
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                NotificationChannel channel = new NotificationChannel(channelId,
                        "Channel human readable title",
                        NotificationManager.IMPORTANCE_DEFAULT);
                assert notificationManager != null;
                notificationManager.createNotificationChannel(channel);
            }
        
            assert notificationManager != null;
            notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-12-31
          • 2011-09-10
          • 2014-04-29
          相关资源
          最近更新 更多