推送通知 - 安卓

问题描述 投票:0回答:4

我的工作,涉及到一个网络界面,可以将信息发送到我的Android应用程序,这将显示如下信息推送通知一个小项目。

但这里是事,我有点混淆如何做到这一点。由于在哪一步将我不得不采取。

所以,我在HTML中具有的通知标题,内容一个文本框网络接口,和一个提交按钮。我想这是当用户点击提交按钮,网页将是s的标题和内容领域的文本发送到我的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);
            }
        });

如果有人能这么好心给我一只手,这将是大加赞赏。

java android web-services web push-notification
4个回答
1
投票

你是对的,到目前为止,一切都与通知栏很好,现在你需要的是一个通知服务,以及谷歌有这样的事情对我们...

enter image description here

how does this works??

在下面的图片看看,

你需要在谷歌服务中注册您的Android应用程序,和Web界面将需要一个ID,所以每次要推的东西到Android,你的web界面,而不是将其与应用的ID推到谷歌的服务器,然后谷歌(不管如何)将本地化您的应用程序,即使它没有运行,他们将得到通知,

幕后有一对夫妇,你必须做的事情,BU没有像推出来自NASA火箭。

我会建议去看看一些tutorials,以便与您的应用程序的注册开始,获得API密钥等等等等。


0
投票

这是在github上的重要来源,其显示如何在Android应用中添加推送通知服务

GitHub.com/冉阿01645/Android-朴实-notification

首先阅读完整的文档

如何添加推送通知,从机器人工作室Android应用程序 - Android开发(部分 - 1连接与火力点)〜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推送通知


0
投票
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);


  }

和的onCreate setNotification();


0
投票

此方法来推送通知

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());
}
© www.soinside.com 2019 - 2024. All rights reserved.