简体   繁体   English

推送通知 - Android

[英]Push Notifications - Android

I am working on a small project that involves a web interface that can send information to my android app which will display such information as Push Notifications.我正在开发一个涉及 Web 界面的小项目,该项目可以将信息发送到我的 android 应用程序,该应用程序将显示诸如推送通知之类的信息。

But here is the thing, I am a bit confused with how to do that.但事情就是这样,我对如何做到这一点感到有些困惑。 As in what step will i have to take.至于我必须采取什么步骤。

So I have a web interface in HTML which has a Textfield for notification Title, Content, and a submit button.所以我有一个 HTML 格式的 Web 界面,它有一个用于通知标题、内容和提交按钮的文本字段。 I want it that when the user clicks the Submit button, the webpage will send the text that s in the Title and Content fields to my android app and then the app will just display them as push notifications.我希望当用户单击提交按钮时,网页会将标题和内容字段中的文本发送到我的 android 应用程序,然后应用程序将它们显示为推送通知。

So far on the app i have it that when you click a button on your device then it just shows a notification on the Actionbar.到目前为止,在应用程序上,当您单击设备上的按钮时,它只会在操作栏上显示通知。 This is great for testing but It would be better that you can just compose your notification through a web interface.这对于测试非常有用,但您最好可以通过 Web 界面编写通知。

My test Push Notification code for the app:我的应用程序测试推送通知代码:

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);
    }
});

If anyone could be so kind to give me a hand, it would be much appreciated.如果有人能这么好心帮我一把,我将不胜感激。

You are right, so far so good with the notification bar, now what you need is a notification service, and google has something like that for us...你是对的,到目前为止通知栏很好,现在你需要的是通知服务,谷歌为我们提供了类似的服务......

在此处输入图片说明

how does this works??这是如何工作的?

Take a look at the image below,看看下面的图片,

you need to register your android app in the google service, and your web interface will need an id, so everytime you want to push something to the android, your web interface instead will push it to the google server with the Id of the app, then google (no matter how) will localize your app, and even if its not running, they will get the notification,您需要在 google 服务中注册您的 android 应用程序,并且您的 Web 界面将需要一个 id,因此每次您想将某些内容推送到 android 时,您的 Web 界面都会将其推送到带有应用程序 ID 的 google 服务器,然后谷歌(无论如何)将本地化您的应用程序,即使它没有运行,他们也会收到通知,

behind the scenes there is a couple of thing that you must do, bu nothing like launching rockets from the NASA.在幕后,您必须做几件事,但没有什么比从 NASA 发射火箭更合适的了。

I will suggest to take a look to some tutorials in order to start with the registration of your app, get the api key etc etc..我会建议查看一些教程,以便从注册您的应用程序、获取 api 密钥等开始。

Here is a great source in github which shows how you can add push notification service in your android app这是 github 中的一个很好的来源,它展示了如何在您的 android 应用程序中添加推送通知服务

github.com/rana01645/android-push-notification github.com/rana01645/android-push-notification

Firstly read the full documentation首先阅读完整的文档

How to add push notification in android application from android studio – Android developer (part – 1 Connect with 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 开发人员(第 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/

How to add push notification in android application from android studio – Android developer (part – 2 Working with server) ~ http://androidrace.com/2017/01/05/how-to-add-push-notification-in-android-application-from-android-studio-android-developer-part-2-working-with-server/如何在 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/

Then you can able to send push notification from your server using html然后您就可以使用 html 从您的服务器发送推送通知

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());

}
}

and call并打电话

  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);


  }

and onCreate setNotification();和 onCreate setNotification();

this method to push notification这种推送通知的方法

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());
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM